Re: [xsl] Union of XPath sequences and the meaning of the uniqueness constraint

Subject: Re: [xsl] Union of XPath sequences and the meaning of the uniqueness constraint
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 10 Oct 2004 21:50:39 +0100
Hi Kenneth,

> The union operator in XPath 2.0 is described as eliminating
> duplicates from the two sequences that it is combining. What is the
> criterion used for determining if two nodes in the resulting
> sequence are unique?

Duplicates are removed based on node identity.

When you set a variable using the content of <xsl:variable>, and don't
use an 'as' attribute, as in:

  <xsl:variable name="a">
    <xsl:sequence select="2" />
  </xsl:variable>

the XSLT processor automatically creates a document node and adds the
result of the sequence constructor inside the <xsl:variable> element
to that document node. When you add an atomic value to a document
node, the atomic value is converted to a text node and that text node
is added.

So, in your code, the variable $a is set to a document node with a
single text node child whose string value is "2". Similarly, $b is set
to a *different* document node with a single text node child whose
string value is "2". These are different document nodes, so "$a | $b"
is a sequence of two document nodes. But "$a | $a" results in a
sequence containing the single document node held by the variable $a.

If you want the variable $a to hold the atomic value "2", then you
should either use the select attribute on <xsl:variable>, as in:

  <xsl:variable name="a" select="2" />

or use the 'as' attribute to say that the variable should hold an
integer, as in:

  <xsl:variable name="a" as="xs:integer">
    <xsl:sequence select="2" />
  </xsl:variable>

If you do this, then you will find that the expressions "$a | $a" and
"$a | $b" will give you an error, since the union operator can only
operate over sequences of nodes.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread