XML technologies / XPath / XPath 2.0: language reference / XPath expressions: operators and keywords / Combining sequences
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)
Operators: |, union, intersect, except
XPath 2.0 provides four operators exclusively for combining node sequences. If any item in either sequence is not a node, a type error occurs.
Union of two sequences
Operators: union or |
The operator union and the XPath 1.0 operator | have the same effect. The operator union was added in XPath 2.0 mainly to accommodate SQL users. Both combine the items of two node sequences into a single sequence:
(a, b, c) union (b, c, d) yields (a, b, c, d)
(a, b, c) | (b, c, d) also yields (a, b, c, d)
The result contains all nodes appearing in either sequence, in document order. If both inputs are empty, the result is empty.
The operator | can also appear within path expressions to express alternatives in intermediate steps. For example, the path expression
/buch/(kapitel | appendix)/abschnitt
produces a sequence in document order of all elements <abschnitt> contained in either <kapitel> or <appendix> elements of the current <buch> element.
Looking back at XPath 1.0
XPath 1.0 has only “|”, with essentially the same function as both XPath 2.0 operators. Its capabilities were expanded: XPath 1.0 cannot use it inside path expressions.
Intersection of two sequences
Operator: intersect
(a, b, c) intersect (b, c, d) yields (b, c)
The result is in document order. If there are no shared items, it is empty.
Looking back at XPath 1.0
XPath 1.0 cannot intersect two node sets, but extension functions exist, such as EXLT’s set:intersection().
XPath 1.0 can nevertheless test whether a node belongs to a node set, albeit awkwardly. Its only node-set operator is union, “|”.
To test whether node $x belongs to node set /beispiel/test, use a construct such as:
/beispiel/test[generate-id(.)=generate-id($x)]
Difference of two sequences
Operator: except
The operator except produces a sequence containing nodes from the first sequence that do not occur in the second:
(a, b, c) except (b, c, d) yields (a)
The result is in document order. If both inputs are identical, it is empty.
Looking back at XPath 1.0
XPath 1.0 cannot compute the difference of two node sets, but extension functions exist, such as EXLT’s set:difference().
Copyright © Galileo Press, Bonn 2008
You may print the online version for private use.
Otherwise, this chapter from "XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn