I have declared a variable in my XSLT as given below :
<xsl:variable name="inline-array">
<Item>A</Item>
<Item>B</Item>
<Item>C</Item>
</xsl:variable>
I am accessing this variable as given below :
<xsl:param name="array"
select="document('')/*/xsl:variable[@name='inline-array']/*" />
<xsl:value-of select="$array[1]" />
This is working fine as long as my inline array has static contents. But my requirement is to dynamically assign values in the XSLT to the tag “Item” ie. Something like :
<xsl:variable name="inline-array">
<Item>$item1</Item>
<Item>$item2</Item>
<Item>$item3</Item>
</xsl:variable>
But, I tried all possible options without any luck. Any suggestions will be greatly appreciated. Any other options to fulfill my requirement is also welcome. Thanks.
One way to achieve this is to make use of an extension function, namely the node-set function, which returns a set of nodes from a result tree fragment.
First you would need to define the namespace for the extension functions like so
In this case, I am using the Microsoft extension functions, but others are available depending on which platform you are using. (http://exslt.org/common is another common one for non-Microsoft platforms).
Next, you define your “array” parameter (or variable, you wanted), like so.
Finally, you can then access this array like so
Putting this altogether in a simple example gives you this
When run, this simply outputs the following result:
1