Xslt stores several variables in scope depending on one test

I have many variables and only two cases. My original approach goes beyond:

<xsl:choose> <xsl:when test='$something = 6'> <xsl:variable name="foo">x</xsl:variable> <!-- 50 other variables --> </xsl:when> <xsl:when test='$something = 7'> <xsl:variable name="foo">y</xsl:variable> <!-- 50 other variables --> </xsl:when> </xsl:choose> 

T. later, with saxon, XPST0008: Variable x has not been declared (or its declaration is not in scope)

I think this will work if I select inside the xsl:variable tag, but then the tests will be repeated again and again and again:

 <xsl:variable name="foo"> <xsl:choose> <xsl:when test='$something = 6'>x</xsl:when> <xsl:when test='$something = 7'>y</xsl:when> </xsl:choose> </xsl:variable> <!-- 50 other variables, the two tests repeated for each... --> 

Is there a way to keep variables in scope, but also not repeat themselves?


update 1

adding full 'sscce' files on request

original approach:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="something">6</xsl:variable> <xsl:choose> <xsl:when test="$something = '6'"> <xsl:variable name="foo">x</xsl:variable> <!-- 50 other variables --> </xsl:when> <xsl:when test="$something = '7'"> <xsl:variable name="foo">y</xsl:variable> <!-- 50 other variables --> </xsl:when> </xsl:choose> <xsl:value-of select="$foo"/> </xsl:template> </xsl:stylesheet> 

which works, but forces itself to repeat:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="something">6</xsl:variable> <xsl:variable name="foo"> <xsl:choose> <xsl:when test='$something = 6'>x</xsl:when> <xsl:when test='$something = 7'>y</xsl:when> </xsl:choose> </xsl:variable> <!-- 50 other variables, the two tests repeated for each... --> <xsl:value-of select="$foo"/> </xsl:template> </xsl:stylesheet> 

Example xml file: <xml/> . saxon command line example: java -jar saxon9he.jar -s:in.xml -xsl:in.xsl -o:out.html

+1
scope xslt xsl-choose
source share
2 answers

Ok, I would rather handle this path:

 <xsl:variable name="something">6</xsl:variable> <xsl:variable name="var.set"> <xsl:choose> <xsl:when test="$something = '6'"> <foo>x</foo> <bar>xx</bar> <!-- 50 other variables, to be inserted as tag --> </xsl:when> <xsl:when test="$something = '7'"> <foo>y</foo> <bar>yy</bar> <!-- 50 other variables, to be inserted as tag --> </xsl:when> </xsl:choose> </xsl:variable> 

var.set will be a node that you can read thanks to the exsl:node-set() extension.

For example, to get the value for foo (stored as node, not as a variable anymore), use something like: <xsl:value-of select="exsl:node-set($var.set)/foo" /> . Like you, you will process one variable as if it were an array of values.

PS: in the root tag of your stylesheet, be sure to add the exsl namespace xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"

+2
source share

If you know your mappings in advance, you can save them in your own file.

So, instead of having something like this

 <xsl:variable name="var1"> <xsl:if test="something = 5">x</xsl:if> </xsl:variable> <xsl:value-of select="$var1"/> 

You may have this

 <xsl:value-of select="document('other.xml')/root/scheme[@number = 5]/@value"/> 

With this as other.xml

 <root> <scheme number="5" value="x"/> <scheme number="6" value="y"/> </root> 

You probably want a more complex other.xml, with different color groups for each color scheme, but the idea would be the same and you could avoid testing and variables completely.

+1
source share

All Articles