Variable Scope in XSLT

I had a problem finding var scoping on xslt. What I really want to do is to ignore the trip tags that have a repeating tour code.

XML example:

<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 Result: Budapest London Rome

+7
variables scope xslt
source share
3 answers

Desired Result : Budapest London Rome

What you need is to group the output by city name. There are two general ways to do this in XSLT.

One of them:

 <xsl:template match="/allTrips"> <xsl:apply-templates select="trip" /> </xsl:template> <xsl:template match="trip"> <!-- test if there is any preceding <trip> with the same <result> --> <xsl:if test="not(preceding-sibling::trip[result = current()/result])"> <!-- if there is not, output the current <result> --> <xsl:copy-of select="result" /> </xsl:if> </xsl:template> 

And the other is called Muenchian grouping, and @Rubens Farias just posted an answer that shows how to do this.

+8
source share

You cannot change variables in XSLT.

You need to think of it more as functional programming instead of procedural, because XSLT is a functional language. Think of a variable scope 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 from the withdrawal? It should be 10 5 , not 10 10 , because temp inside the other function is not the same variable as temp outside this function.

This is the same in XSLT. The variables created after this cannot be overridden, because they are write-once, read-many variables by design.

If you want to make a conditional conditional value of a variable, you need to conditionally define the variable:

 <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> 

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

+24
source share

Try the following:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="trip" match="trip" use="result" /> <xsl:template match="/trips"> <xsl:for-each select="trip[count(. | key('trip', result)[1]) = 1]"> <xsl:if test="position() != 1">, </xsl:if> <xsl:value-of select="result"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> 
+4
source share

All Articles