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 were generalized so that any expression can be used as an operand of /, (both on the left and the right), so long as the value of the left-hand operand 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 items (but not a mixture of the two). This allows constructs such as $x/number(), which returns the sequence obtained by converting each item in $x to a number.

Axis steps

The basic primitive for accessing a source document is the axis step. Axis steps may be combined into path expressions using the path operators / and //, and they may be filtered using filter expressions in the same way as the result of any other expression.

An axis step has the basic form axis :: node-test, and selects nodes on a given axis that satisfy the node-test. The axes available are:

ancestor

Selects ancestor nodes starting with the current node and ending with the document node

ancestor-or-self

Selects the current node plus all ancestor nodes

attribute

Selects all attributes of the current node (if it is an element)

child

Selects the children of the current node, in document order

descendant

Selects the children of the current node and their children, recursively (in document order)

descendant-or-self

Selects the current node plus all descendant nodes

following

Selects the nodes that follow the current node in document order, other than its descendants

following-or-self

Selects the current node and all following nodes

following-sibling

Selects all subsequent child nodes of the same parent node

following-sibling-or-self

Selects the current node and all following sibling nodes

namespace

Selects all the in-scope namespaces for an element (this axis is deprecated in the W3C XPath specification, but Saxon will continue to support it)

parent

Selects the parent of the current node

preceding

Selects the nodes that precede the current node in document order, other than its ancestors

preceding-or-self

Selects the current node and all preceding nodes

preceding-sibling

Selects all preceding child nodes of the same parent node

preceding-sibling-or-self

Selects the current node and all preceding sibling nodes

self

Selects the current node

The four new XPath 4.0 axes are available from Saxon 13: preceding-or-self, preceding-sibling-or-self, following-or-self, and following-sibling-or-self.

When the child axis is used, child:: may be omitted, and when the attribute axis is used, attribute:: may be abbreviated to @. The expression parent::node() may be shortened to ..

The expression . is no longer synonymous with self::node(), since it may now select items that are not nodes. If the context item is not a node, any use of a path expression will raise an error.

The node-test may be, for example: