XSLT: Set multiple variables depending on condition
Asked Answered
C

2

11

I want to assign multiple variables depending on one condition environment. I know how to do that for only one variable:

<xsl:variable name="foo">
    <xsl:choose>
        <xsl:when test="$someCondition">
            <xsl:value-of select="3"/>
        <xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="4711"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

But what if I want to assign two variables depending on the same condition $someCondition?

I don't want to write the same xsl:choose statement again, because it is somewhat lengthy and computation intensive in the real example.

The environment in question is libxslt (xslt 1.0) with exslt extensions.

EDIT: What i want is a behaviour similar to

if (condition) {
    foo = 1;
    bar = "Fred";
}
else if (...)  {
    foo = 12;
    bar = "ASDD";
}
(... more else ifs...)
else {
    foo = ...;
    bar = "...";
}
Canoodle answered 7/9, 2012 at 12:21 Comment(0)
L
11

What you could is have your main variable return a list of elements; one for each variable you want to set

  <xsl:variable name="all">
     <xsl:choose>
        <xsl:when test="a = 1">
           <a>
              <xsl:value-of select="1"/>
           </a>
           <b>
              <xsl:value-of select="2"/>
           </b>
        </xsl:when>
        <xsl:otherwise>
           <a>
              <xsl:value-of select="3"/>
           </a>
           <b>
              <xsl:value-of select="4"/>
           </b>
        </xsl:otherwise>
     </xsl:choose>
  </xsl:variable>

Then, using the exslt function, you can convert this to a 'node set' which can then be used to set your individual variables

  <xsl:variable name="a" select="exsl:node-set($all)/a"/>
  <xsl:variable name="b" select="exsl:node-set($all)/b"/>

Don't forget you'll need to declare the namepsace for the exslt functions in the XSLT for this to work.

Lajoie answered 7/9, 2012 at 12:37 Comment(0)
C
3

But what if I want to assign two variables depending on the same condition $someCondition?

I don't want to write the same xsl:choose statement again, because it is somewhat lengthy and computation intensive in the real example.

Assuming the values of the variables are not nodes, this code doesn't use any extension function to define them:

<xsl:variable name=vAllVars>   
     <xsl:choose> 
        <xsl:when test="$someCondition"> 
            <xsl:value-of select="1|Fred"/> 
        <xsl:when> 
        <xsl:when test="$someCondition2"> 
            <xsl:value-of select="12|ASDD"/> 
        <xsl:when> 
        <xsl:otherwise> 
            <xsl:value-of select="4711|PQR" />
        </xsl:otherwise> 
    </xsl:choose> 
</xsl:variable>   

<xsl:variable name="foo" select="substring-before($vAllVars, '|')"/>
<xsl:variable name="bar" select="substring-after($vAllVars, '|')"/>
Chalybeate answered 7/9, 2012 at 12:42 Comment(7)
Using this, foo and bar would always have the same value. That is not what I need... See my edit for clarification.Canoodle
@Jost, Yes, see my update. Whenever the values of the variables aren't nodes, it is easier and probably more efficient not to build (and convert) a result tree at all.Chalybeate
this solution should work. There is a drawback however: You need a separator which can never occur in any of the variable values, which may be hard to find. And after choosing one, this would be a place where stuff could break in the future when some other part of the program changes...Canoodle
@Jost: Use something like: $#@!321 :)Chalybeate
Then it is guaranteed that someone will use this character. :-)Canoodle
@Jost, It is not "a character" -- it is a string and can have many characters. We know that if we give all the monkeys a typewriter (each) and they type for 200 years, they will write all the works of Shakespeare. However, given that our programs are most likely intended to be used for 10 years or less, even the monkeys are not a threat. Did I mention that one can use a GUID for a delimiter? Nobody can invent this in advance.Chalybeate
@Jost, This solution has one more, serious, advantage: The code that sets the values doesn't need to know the names of the variables -- these values can be assigned to different variable names by the outer code.Chalybeate

© 2022 - 2024 — McMap. All rights reserved.