The scope of the xslt variable and its use

I am studying xslt and asked one question about how to use the xslt variable in diff. for each cycle. I know that xslt is not a procedural language, so a variable declared in a for loop cannot be accessed in another loop. But is it possible to somehow declare a global variable, and then assign some value in the first to the loop and use this variable in the second loop?

Any ideas would be highly appreciated.

thanks

+6
source share
3 answers

Is there a way that I can simply declare a global variable and then assign some value in the first for the loop and use this variable in the second for the loop?

The way to assign xsl:variable (of course, this is just initialization) from within xsl:for-each is to include xsl:for-each in the body of the variable .

Here is a complete example:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <xsl:variable name="vMax"> <xsl:for-each select="num"> <xsl:sort data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="."/> </xsl:if> </xsl:for-each> </xsl:variable> Values close to the maximum: <xsl:text/> <xsl:for-each select="num"> <xsl:if test="not($vMax - . > 3) "> <xsl:value-of select="."/> <xsl:text>&#xA;</xsl:text> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet> 

When this conversion is applied to the following XML document ...

 <nums> <num>01</num> <num>02</num> <num>03</num> <num>04</num> <num>05</num> <num>06</num> <num>07</num> <num>08</num> <num>09</num> <num>10</num> </nums> 

... it first defines the vMax variable, which gets its value from xsl:for-each contained in its body.

Then the vMax variable vMax used in the second xsl:for-each to output all numbers that are "close" to the calculated maximum maximum.

The obtained, correct result is obtained :

  Values close to the maximum: 07 08 09 10 

You can also model the "assignment" of a variable with different values ​​using a recursively named named template and pass the "new value" as a parameter to the called template .

Here is an example showing this technique. Here we calculate the maximum values ​​contained in the nodes of a node -set. Each time we access the next node in node-set, the current maximum is compared with this value, and if necessary, the new maximum becomes the value of the next node. Then we call the same pattern recursively, passing the new maximum as the value of the current maximum:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <xsl:call-template name="max"> <xsl:with-param name="pList" select="*"/> </xsl:call-template> </xsl:template> <xsl:template name="max"> <xsl:param name="pMax" select="-99999999"/> <xsl:param name="pList"/> <xsl:choose> <xsl:when test="$pList[1]"> <xsl:variable name="vnewMax" select= "$pMax * ($pMax >= $pList[1]) + $pList[1] * not($pMax >= $pList[1]) "/> <xsl:call-template name="max"> <xsl:with-param name="pMax" select="$vnewMax"/> <xsl:with-param name="pList" select="$pList[position() > 1]"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$pMax"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> 

When this conversion is applied to the same XML document (above), the desired, correct result is obtained :

 10 
+13
source

xsl:for-each not a loop in the sense of for or foreach there are loops in procedural languages, so any question about loops is hard to understand and more difficult to answer.

If you want to use global variables in XSLT, you can do it, but you bind the value to the variable in which you declare it (i.e. globally), you cannot assign a value later in for-each , since you seem to want to do it.

0
source
  • You wrote: "XSL is a procedural language." Well, he is not. This is a declarative language ..
  • A variable is assigned with its declaration, variables do not change!
  • Usually we make a recursive call for templates using a call-template passing parameters to them .. (this works like calling a recursive function with passing arguments in procedural languages)
    This is one of the methods for processing samples and conditional cycles, etc.

We will be happy to help you figure it out if you specify the exact script with the XML sample and the output that you expect from it :)

0
source

Source: https://habr.com/ru/post/908260/


All Articles