Combining Results

Your query results may consist of several FLWORs or other expressions that each return a result sequence. In addition, the sequences you use in your for and let clauses may be composed from more than one sequence.

There are four ways to combine two or more sequences to form a third sequence. They differ in which items are selected, whether their order is affected, whether duplicates are eliminated, and whether atomic values are allowed in the sequences.

Sequence Constructors

The first way to merge two sequences is simply to create a third sequence that is the concatenation of the first two. This is known as a sequence constructor, and it uses parentheses and commas to concatenate two sequences together. For example:

let $prods := doc("catalog.xml")//product
let $items := doc("order.xml")//item
return ($prods, $items)

returns a sequence that is the concatenation of two other sequences, $prods and $items. The items in $prods are first in the sequence, then the items in $items, in the order they appear in that sequence. No attempt is made to eliminate duplicates or sort the items in any way.

Note that concatenation is the only way to combine sequences that contain atomic values; union, intersect, and except work on sequences that contain nodes only.

The union Expression

Another approach to combining sequences of nodes is via a union expression, which is indicated by the keyword union or the vertical bar character (|). The two operators have the exact same meaning. ...

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.