Set Operations

XML Path Language (XPath) supports the set operation |.

Union (|) Operator

The |, or union, operator returns the union of its two operands, which must be node-sets. For example, //author | //publisher returns a node-set that combines all the //author nodes and all the //publisher nodes. Multiple union operators can be chained together to combine multiple node-sets. For example, //author | //publisher | //editor | //book-seller returns a node-set containing all //author, //publisher, //editor, and //book-seller elements. The union operator preserves document order and does not return duplicates.

Examples

Expression Refers to

first-name | last-name

A node set containing <first-name> and <last-name> elements in the current context.

(bookstore/book | bookstore/magazine)

A node set containing <book> or <magazine> elements inside a <bookstore> element.

book | book/author

A node set containing all <book> elements and all <author> elements within <book> elements.

(book | magazine)/price

The node set containing all <price> elements of either <book> or <magazine> elements.

Example

The following example illustrates the effect of the union operator.

XML File (test.xml)

XSLT File (test.xsl)

The following XSLT style sheet selects all the <x> elements whose a attribute is equal to 2, plus those <x> elements that have no attributes.

The transformation yields the following result:

Precedence

Precedence order (from highest precedence to lowest) between Boolean and comparison operators is shown in the following table.

Precedence Operators Description

1

( )

Grouping

2

[ ]

Filters

3

/

//

Path operations

4

&lt;

&lt;=

&gt;

&gt;=

Comparisons

5

=

!=

Comparisons

6

|

Union

7

not()

Boolean not

8

and

Boolean and

9

or

Boolean or

Example

The following example illustrates the effect of the operator precedence listed above.

XML File (test.xml)

Basic XSLT File (test.xsl)

We will use this basic XSLT file as a starting point for the series of illustrations that follow.

Case 0. Test run

You can add the following template-rule to the XSLT style sheet.

This will produce an XML document identical to the original one, without the <?xml version="1.0"?> processing instruction.

The following cases show different ways of writing this template rule. The point is to show the order in which the XPath operators bind to an element.

Case 1: () binds tighter than []

The following template rule selects the first <y> element in the document order, from all the <y> elements in the source document.

The result is as follows:

<y>y31</y>

Case 2: [] binds tighter than / or //

The following template rule selects all the <y> elements that are the first among their siblings.

The result is as follows:

<y>y31</y>

<y>y21</y>

<y>y11</y>

<y>y03</y>

Case 3: and, not

The following template rule selects all the <x> elements that have no <x> child elements, that have an <x> parent element, and that do not have any attributes.

The result is a single <x> element, listed below with its children:

<x>
   <y>y31</y>
   <y>y32</y>
</x>

Case 4: or, and, not

The following template rule selects each <x> elements that is a child of an <x> element; or, that is not a parent of an <x> element and has no attributes.

The result is a node set containing the following <x> elements, listed below with its children:

Case 5: and, or, not

The following template rule selects each <x> element that is a child of an <x> element but not a parent of an <x> element; or, that has no attributes.

The result is a node set containing the following <x> elements, listed below with its children: