Home
Atom Feed

Conditional <xsl:variable> and <xsl:value-of />

When you make conditional variables in xslt, the value of the variable becomes a string if you use the <xsl:value-of select /> statement.

Consider the code below:

<xsl:variable name="SourceXML">
 <xsl:for-each select="umbraco.library:Split($source, ',') /value">
 <xsl:value-of select="umbraco.library:GetXmlNodeById(current()/text())"/>
 </xsl:for-each>    
 </xsl:variable>

Here, the value of "$SourceXML" is a string. This might not always be a problem, but in this case, $SourceXML is supposed to be a collection of nodes.

Trying to convert it to a node collection by encasing it in a msxsl:node-set() will not help.

The solution is to use <xsl:copy-of select /> instead, and then convert it to a node-set:

<xsl:variable name="SourceXML">
 <xsl:for-each select="umbraco.library:Split($source, ',') /value">
 <xsl:copy-of select="umbraco.library:GetXmlNodeById(current()/text())"/>
 </xsl:for-each>    
 </xsl:variable>

<xsl:variable name="SourceXMLNodeSet" select="msxsl:node-set($SourceXML)"/>