Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable scope in XSLT

I am having an issue trying to figure out var scoping on xslt. What I actually want to do it to ignore 'trip' tags that have a repeated 'tourcode'.

Sample XML:

<trip>
 <tourcode>X1</tourcode>
 <result>Budapest</result>
</trip>
<trip>
 <tourcode>X1</tourcode>
 <result>Budapest</result>
</trip>
<trip>
 <tourcode>X1</tourcode>
 <result>Budapest</result>
</trip>
<trip>
 <tourcode>Y1</tourcode>
 <result>london</result>
</trip>
<trip>
 <tourcode>Y1</tourcode>
 <result>london</result>
</trip>
<trip>
 <tourcode>Z1</tourcode>
 <result>Rome</result>
</trip>

XSLT Processor:

<xsl:for-each select="trip">    
    <xsl:if test="not(tourcode = $temp)">
      <xsl:variable name="temp" select="tour"/>
      // Do Something (Print result!)
    </xsl:if>
</xsl:for-each>

Desired Output: Budapest london Rome

like image 431
Mazzi Avatar asked Feb 05 '10 01:02

Mazzi


1 Answers

You can't change variables in XSLT.

You need to think about it more as functional programming instead of procedural, because XSLT is a functional language. Think about the variable scoping in something like this pseudocode:

variable temp = 5
call function other()
print temp

define function other()
  variable temp = 10
  print temp

What do you expect the output to be? It should be 10 5, not 10 10, because the temp inside the function other isn't the same variable as the temp outside that function.

It's the same in XSLT. Variables, once created, cannot be redefined because they are write-once, read-many variables by design.

If you want to make a variable's value defined conditionally, you'll need to define the variable conditionally, like this:

<xsl:variable name="temp">
  <xsl:choose>
    <xsl:when test="not(tourcode = 'a')">
      <xsl:text>b</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>a</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<xsl:if test="$temp = 'b'">
  <!-- Do something -->
</xsl:if>

The variable is only defined in one place, but its value is conditional. Now that temp's value is set, it cannot be redefined later. In functional programming, variables are more like read-only parameters in that they can be set but can't be changed later. You must understand this properly in order to use variables in any functional programming language.

like image 57
Welbog Avatar answered Dec 02 '22 19:12

Welbog