Hello everybody, I’ve working on an XSLT Stylesheet for a while and had no clue trying to access the child nodes of a variable containing data generated using eXSLT node-set (che what I’m taling about at http://exslt.org/exsl/functions/node-set/index.html).
You know, eXSLT is a great thing, it can give you some functionalities which are missing in XSLT 1.0 and that you can find only in higher XSLT version, which are not so oftern implemented. Luckily, PHP 7 and Libxml support it!
But go back to work! I was declaring a second variable with a subset of the nodes from the orignal variable and I was unable to access its children nodes.
Let’s assume that we have a variable declared as:
<xsl:variable name="sorted_rows"> <xsl:for-each select="$originalRows"> <!-- you can replace this with any XPath expression --> <xsl:sort select="MyFieldToSort" /> <xsl:element name="NewKindOfRow"> <xsl:copy-of select="node()|@*" /> </xsl:element> </xsl:for-each> </xsl:variable>
Then, I wanted a subset of those rows so I defined another variable in this way:
<xsl:variable name="mysubset"> <xsl:value-of select="exsl:node-set($sortedRows)/NewKindOfRow[Description = 'description to find']" /> </xsl:variable>
But I couldn’t iterate over them or access any child node. Digging and debugging I found that the variable’s type wasn’t what I expected. I used the eXSLT common “object-type” function and I noticed that:
<xsl:value-of select="exsl:object-type($mysubset)" />
was giving “RTF” as result, instead of “node-set” which was the value I was expecting. For more info about eXSLT object type visit http://exslt.org/exsl/functions/object-type/index.html.
So, what’s wrong ? I found that you MUST define a new variable in THIS way, as one-line definition, when working with:
<xsl:variable name="mysubset" select="exsl:node-set($sortedRows)/NewKindOfRow[Description = 'description to find']" />
Defining a variable with both <variable> and <value-of> tags still works while working with normal XPath expression.