XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (426 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
5.26Mb size Format: txt, pdf, ePub

These are sometimes called set operators, but the word “set” is so overloaded that I prefer the term
Venn operators
, after the nineteenth-century English mathematician who popularized the diagrams used to explain them.

The XDM data model does not actually support sets (that is, collections with no intrinsic order and with duplicates disallowed). Instead, sets of nodes are simulated using sequences. The actual type of the operands for the
union
,
intersect
and
except
operators is
node()*
, which allows any sequence of nodes. This is also the type of the result. But semantically, these operators ignore any duplicates in the input sequence, and they ignore the order of the nodes in the input sequence. The result sequence will never contain any duplicates, and the results will always be in document order. The effect of the three operators is illustrated in
Figure 9-1
.

As with path expressions, when we talk about duplicate nodes in this section, we are always talking about multiple references to the same node, that is, we are concerned with node identity, not with the values contained in the nodes. Unfortunately, XDM talks about sequences containing nodes, when it really means that sequences contain references to nodes. I personally feel uncomfortable talking about a sequence containing two nodes that have the same identity, or containing the same node more than once: I find it much more natural to talk about a sequence containing two references to the same node.

Users often questioned whether the node-sets of XPath 1.0 were true sets, given that (in XSLT at any rate) the nodes were always processed in document order. The answer is that they were indeed true sets, because it was not possible to have distinct collections such as (A, B), (B, A), and (A, B, B, A). There was only one collection in the XPath 1.0 model that could contain the nodes A and B, and the fact that the nodes were always processed in a canonical order doesn't change this.

In XPath 2.0, it is possible to have distinct sequences such as (A, B), (B, A), and (A, B, B, A). However, the operators described in this section treat these sequences as if they were identical. So these operators are using sequences to simulate node-sets, and I shall therefore use the term node-sets to describe these values.

Syntax

Expression
Syntax
UnionExpr
IntersectExceptExpr ( (
union
|
|
) IntersectExceptExpr )*
IntersectExceptExpr
PathExpr ( (
intersect
|
except
) PathExpr )*

This syntax shows that the
union
operator (which has
|
as a synonym) binds less tightly than the
intersect
and
except
operators. So the expression
A union B intersect C
means
A union (B intersect C)
. As always, there is no shame in adding extra parentheses if you're not sure about the rules (or even if you are).

Other books

Hotel For Dogs by Lois Duncan
The Last Oracle by James Rollins
The Master of Confessions by Thierry Cruvellier
So Close to Heaven by Barbara Crossette
The Lives Between Us by Theresa Rizzo