Expressions for combining sequences of nodes

Db2 XQuery provides operators for combining sequences of nodes. These operators include union, intersect, and except.

The following table describes the operators that are available for combining sequences of nodes.

Table 1. XQuery operators for combining sequences of nodes
Operator Description
union or | Takes two node sequences as operands and returns a sequence that contains all of the nodes that occur in either of the operands. The union keyword and the | character are equivalent.
intersect Takes two node sequences as operands and returns a sequence that contains all of the nodes that occur in both operands.
except Takes two node sequences as operands and returns a sequence that contains all of the nodes that occur in the first operand but not in the second operand.

All of these operators eliminate duplicate nodes from their result sequences based on node identity. The resulting sequence is returned in document order.

The operands of union, intersect, or except must resolve to sequences that contain nodes only. If an operand contains an item that is not a node, an error is returned.

In addition to the operators that are described in this topic, Db2 XQuery provides functions for indexed access to items or sub-sequences of a sequence (fn:index-of), for indexed insertion or removal of items in a sequence (fn:insert-before and fn:remove), and for removing duplicate items from a sequence (fn:distinct-values).

Examples

In these examples, suppose that the variable $managers is bound to a set of employee nodes that represent employees who are managers, and the variable $students is bound to a set of employee nodes that represent employees who are students.

The following expressions are all valid examples that use operators to combine sequences of nodes:
  • $managers union $students returns the set of nodes that represent employees who are either managers or students.
  • $managers intersect $students returns the set of nodes that represent employees who are both managers and students.
  • $managers except $students returns the set of nodes that represent employees who are managers but not students.