Reputation: 1091
I want to get the value "a" in the node a under Root tag, within the output document(Input can be anything). I know that if I do
<xsl:value-of select="$item1"/>
I will get the desired value. However i want to use something like
<xsl:value-of select="concat('$item','1')"/>
The reason is because I can have many variables created, dynamically, and the number at the end of the variable gets incremented. So I can have item1,item2,item3 etc. I have shown a sample here, that is why I use hardcoded value , '1', in the value of select. Is this possible in xslt1.0?
Here is my xslt, any input xml can be used
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="item1" select="'a'" />
<Root>
<a>
<xsl:value-of select="concat('$item','1')"/>
</a>
</Root>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 667
Reputation: 1287
PHP like variable variables aren't possible in XSLT 1.0 / XPath 1.0.
With the node-set()
function of the exslt
-extension you can build a node-set which works like an array.
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:exsl='http://exslt.org/common'
xmlns:msxsl='urn:schemas-microsoft-com:xslt'
exclude-result-prefixes='msxsl exsl'>
<xsl:template match='/'>
<!-- result tree fragment -->
<xsl:variable name='_it'>
<em>a</em>
<em>b</em>
<em>c</em>
<em>d</em>
</xsl:variable>
<!-- create a node-set from the result tree fragment -->
<xsl:variable name='it' select='exsl:node-set($_it)'/>
<Root>
<a>
<!--
this is a normal xpath with the variable '$it' and a node 'em'
the number in brackets is the index starting with 1
-->
<xsl:value-of select='$it/em[1]'/> <!-- a -->
<xsl:value-of select='$it/em[2]'/> <!-- b -->
</a>
</Root>
</xsl:template>
<!-- MS doesn't provide exslt -->
<msxsl:script language='JScript' implements-prefix='exsl'>
this['node-set'] = function (x) {
return x;
}
</msxsl:script>
</xsl:stylesheet>
Upvotes: 1