Search code examples
xsltlibxslt

xslt variable strangeness


Can anyone explain this to me? (using latest libxslt):

 <a><xsl:copy-of select="(@attrib|exsl:node-set(0))"/></a>
 <b><xsl:copy-of select="(@attrib|exsl:node-set(0))[position()=1]"/></b>

 <x><xsl:copy-of select="(@attrib|exsl:node-set(0))[1]"/></x>
 <xsl:variable name="value" select="@attrib"/>
 <y><xsl:copy-of select="($value|exsl:node-set(0))[1]"/></y>

Result (for @attrib = 1 at current node):

 <a attrib="1">0</a>
 <b attrib="1"/>

 <x>0</x>
 <y attrib="1"/>

<a> and <b> show expected behavior.
<x> is IMHO incorrect.
But why does putting @attrib into a variable "fix" it for <y>?

BTW: Everything is correct when @attrib is not present. The copy-of used here is for debugging; original usage converts the XPath result to a number, and a missing attribute shall not lead to NaN, but a certain default value.


Solution

  • This version of exsl:node-set() presumably creates a node that is in a different tree from from the node @attrib. When two nodes are in different trees, it is implementation-dependent which of them comes first in document order. The construct (X|Y)[position()=1] (or equivalently (X|Y)[1]) selects whichever of X and Y comes first in document order. Therefore it's essentially unpredictable whether it selects X or Y.