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> </xsl:when> <xsl:when test='$something = 7'> <xsl:variable name="foo">y</xsl:variable> </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>
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> </xsl:when> <xsl:when test="$something = '7'"> <xsl:variable name="foo">y</xsl:variable> </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> <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
scope xslt xsl-choose
n611x007
source share