Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:37:45+00:00 2026-05-15T15:37:45+00:00

<xsl:choose> <xsl:when test=long convoluted expression> <xsl:element name=Class>123</xsl:element> <a lot more xsl:elements> </xsl:when> <xsl:when test=next

  • 0
<xsl:choose>
    <xsl:when test="long convoluted expression">
        <xsl:element name="Class">123</xsl:element>
        <a lot more xsl:elements>
    </xsl:when>
    <xsl:when test="next very long expression">
        <xml:element name="Class">124</xsl:element>
        <a lot more xsl:elements>
    </xsl:when>
    <tens of more similar xsl:when>
</xsl:choose>

Is there a way to simplify the above code with conditionals? For every class value the objects are given, there follows tens of rows with extra attributes. These attributes form sets according to the value of the class. Class 0-99 has one set of extra tags, class 100-199 a second, forming a maintenance nightmare when one of these extra tag sets change.

I was considering a solution like this:

<xsl:choose>
    <xsl:when test="long convoluted expression">
        <xml:element name="Class">123</xsl:element>
        <xsl:variable name="outputclass" select="123">
    </xml:when>
    <xsl:when test="next very long expression">
        <xml:element name="Class">124</xsl:element>
        <xsl:variable name="outputclass" select="124">
    </xsl:when>
</xsl:choose>
<xsl:choose>
    <xsl:when test="$outputclass > 99">
        <xml:elements for classes 100-199 here>
    </xsl:when>
<xsl:choose>

But of course this fails, as the outputclass variable is not in the same scope. Any way to go around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:37:46+00:00Added an answer on May 15, 2026 at 3:37 pm

    The best solution to this problem is well-known:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
        <xsl:variable name="voutType">
          <xsl:choose>
            <xsl:when test="long convoluted expression">123</xsl:when>
            <xsl:when test="next very long expression">124</xsl:when>
            <!-- Etcetera ... -->
          </xsl:choose>
        </xsl:variable>
    
       <Class><xsl:value-of select="$voutType"</Class>
             <xsl:choose>
                <xsl:when test="not($voutType > 99)">
                    <!-- elements for classes 0 - 99 here -->
                </xsl:when>
                <xsl:otherwise>
                    <!-- elements for classes 100-199 here -->
                </xsl:otherwise>
             <xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    Do note:

    1. In order to give values to a variable ($voutType), the <xsl:choose> instruction must be inside the body of the <xsl:variable>

    2. You only need to specify the <Class> element once — outside of everything else.

    3. You don’t have to use <xsl:element> if the element name is known.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have this in xsl: <xsl:choose> <xsl:when test=something > 0> <xsl:variable name=myVar select=true()/>
<xsl:choose> <xsl:when test=block4[messageType = '103']> <xsl:value-of select=block4/tag[name = '32A']/value/>,<xsl:text/> </xsl:when> <xsl:when test=block4[messageType = '102']>
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
Can you nest xsl:if within nested xsl:when, for example: <xsl:choose> <xsl:when test=param/@id = '1'
Is there any way to optimize this code. <xsl:choose> <xsl:when test=val1 = val2> <xsl:apply-templates
Using c#3 compiled transforms the following seems to work just fine... <xsl:choose> <xsl:when test=$valA
What can I do to make this code work? <xsl:choose> <xsl:when test='type = 6'>
Trying to combine an if statement using <xsl:if test=expression> where I want to have
<xsl:choose> <xsl:when test=/doc/paymentHistory/paymentList/paymentlistlength='1> <script language=JavaScript type=text/javascript> javascript:OpenBrWindow('portal?cmd=dekontView&amp;AVPaymentDate=document.getElementsByName('AVPaymentDate')&amp;lang=tr','Preview','resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no',990, 590,'true'); return false; </script> <xsl:when> </xsl:choose> After
I have an XSL template: <meta name=description content={$page-description}/> and a variable: <xsl:variable name=page-description> <xsl:choose>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.