i have many variables , 2 cases. original approach goes out of scope:
<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>
ie. later, saxon, xpst0008: variable x has not been declared (or declaration not in scope)
i think work if choose inside xsl:variable
tag, tests repeated on , on , on , on , over:
<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, 2 tests repeated each... -->
is there way keep variables in scope don't repeat myself?
update 1
adding complete '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>
approach works forces repeat myself:
<?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, 2 tests repeated each... --> <xsl:value-of select="$foo"/> </xsl:template> </xsl:stylesheet>
example xml file: <xml/>
. example saxon command-line: java -jar saxon9he.jar -s:in.xml -xsl:in.xsl -o:out.html
well, i'd rather process way :
<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, inserted tag --> </xsl:when> <xsl:when test="$something = '7'"> <foo>y</foo> <bar>yy</bar> <!-- 50 other variables, inserted tag --> </xsl:when> </xsl:choose> </xsl:variable>
the variable var.set
nodeset able read exsl:node-set()
extension.
for example retrieve value foo
(stored node , not variable anymore), use like: <xsl:value-of select="exsl:node-set($var.set)/foo" />
. handle single variable, quite if array of values.
ps: on root tag of stylesheet, not forget add exsl
namespace declaration xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"
Comments
Post a Comment