Search code examples
xmlxsltexsltnode-set

XSLT - Iterate through node-set


I have a xml like this:

<root>
<row>
    <col name="col1">&lt;root&gt;&lt;row&gt;&lt;col name=&quot;COL_NAME_1&quot;&gt;col_value_1&lt;/col&gt;;&lt;/row&gt;&lt;/root&gt;</col>
    <col name="col2">&lt;root&gt;&lt;row&gt;&lt;col name=&quot;COL_NAME_2&quot;&gt;col_value_2&lt;/col&gt;;&lt;/row&gt;&lt;/root&gt;</col>
</row></root>

I need to get escaped collection from col1 and iterate through its rows. I'm using exsl:node-set function. Here is my simplified xsl:

<xsl:template match="/">
<xsl:variable name="collection" select="exsl:node-set(./root/row/col[@name='col1'])" disable-output-escaping="yes" />
<xsl:for-each select="($collection)/root/row">
    <!-- ... -->
</xsl:for-each></xsl:template>

I can correctly read value from variable $collection using xsl:value-of function but i can't iterate over it as if there were no rows. Any ideas what am I doing wrong?


Solution

  • If the author of this XML had wanted you to see elements like root and row inside the col element, they wouldn't have escaped the angle brackets. Because they are escaped, there's no structure here, only text. The operation of getting structure from the text is called parsing. XSLT 3.0 has a function parse-xml() to parse text and create a tree structure; earlier versions don't, though your vendor might provide extensions or you might be able to write your own.