Node access functions
One part of the sequence type syntax can also be used in path expressions in order to filter out some nodes from the sequence of the nodes which have been addressed by a path expression. For this purpose, the node() type as well as any more specific types according to the image "Hierarchy of sequence types" are available. The rules for the selection correspond to the ones mentioned before. Therefore, there is a difference between the expression //Employee and the expression //element(Employee). The first expression searches for an element with the name Employee, the second expression searches for an element from the substitution group of the element Employee defined in a known schema.
Operations on sequences of nodes
Sequences of nodes can be combined with each other by generating the average, the union or the difference. Sequences are not sets, but for these operations they are considered temporarily as a set. Duplicates (also identical nodes) among the items are eliminated, then the appropriate set operations are carried out. The result is provided in the document order. The syntax for these operators is as follows:
UnionExpr | ::= | IntersectExceptExpr union IntersectExceptExpr |
IntersectExceptExpr | ::= | ValueExpr (intersect | except) ValueExpr |
Instead of the union operator, also a vertical line »|« can be used. In the following example the $a variable is bound to all element nodes of the name A in the example document, the $b variable to all element nodes of the name B, but in reversed order. The expression
let $doc := document {<Root>
<A>1</A>
<B>2</B>
<A>3</A>
<B>4</B>
</Root>}
let $a := $doc//A
let $b := fn:reverse($doc//B)
return
(<A-Sequence>{$a}</A-Sequence>,
<B-Sequence>{$b}</B-Sequence>,
<Union>{$a | $b}</Union>,
<Difference>{$b except $a}</Difference>,
<Average>{$b intersect $b}</Average>)
results in:
<A-Sequence> <A>1</A> <A>3</A></A-Sequence>
<B-Sequence> <B>4</B> <B>2</B></B-Sequence>
<Union> <A>1</A><B>2</B><A>3</A><B>4</B></Union>
<Difference><B>2</B> <B>4</B></Difference>
<Average><B>2</B><B>4</B></Average>
As can be seen, the results of the sequence operations union, intersect and except are always sorted according to the document order.
It should be noted here that these operators are not defined for any sequences, but only for sequences which only consist of nodes.
Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)
<< back | next >> |