Path expressions

A path expression is a sequence of steps separated by the / or // operator. For example, ../@desc selects the desc attribute of the parent of the context node.

In XPath 2.0, path expressions have been generalized so that any expression can be used as an operand of /, (both on the left and the right), so long as its value is a sequence of nodes. For example, it is possible to use a union expression (in parentheses) or a call to the id() or key() functions. The right-hand operand is evaluated once for each node in the sequence that results from evaluating the left-hand operand, with that node as the context item. In the result of the path expression, nodes are sorted in document order, and duplicates are eliminated.

In practice, it only makes sense to use expressions on the right of / if they depend on the context item. It is legal to write $x/$y provided both $x and $y are sequences of nodes, but the result is exactly the same as writing ./$y.

Note that the expressions ./$X or $X/. can be used to remove duplicates from $X and sort the results into document order. The same effect can be achieved by writing $X|()

The operator // is an abbreviation for /descendant-or-self::node()/. An expression of the form /E is shorthand for root(.)/E, and the expression / on its own is shorthand for root(.).

The expression on the left of the / operator must return a node or sequence of nodes. The expression on the right can return either a sequence of nodes or a sequence of atomic values (but not a mixture of the two). This allow constructs such as $x/number(), which returns the sequence obtained by converting each item in $x to a number.