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:
-
a node name
In XPath 4.0, the default namespace for elements and types can be set to the value
##any, allowing unprefixed names in axis steps to match elements with a given local name in any namespace. -
prefix:*to select nodes in a given namespace -
*:localnameto select nodes with a given local name, regardless of namespace -
text()to select text nodes -
node()to select any node -
processing-instruction()to select any processing instruction -
processing-instruction('literal')to select processing instructions with the given name (target) -
comment()to select comment nodes -
element()orelement(*)to select any element node -
element(N)to select any element node namedN -
element(*, T)to select any element node whose type annotation isT, or a subtype ofT -
element(N, T)to select any element node whose name isNand whose type annotation isT, or a subtype ofT -
schema-element(N)to select any element node whose name isN, or an element in the substitution group ofN, that conforms to the schema-defined type for a global element declaration namedNin an imported schema -
attributeorattribute(*)to select any attribute node -
attribute(N)to select any attribute node namedN -
attribute(*, T)to select any attribute node whose type annotation isT, or a subtype ofT -
attribute(N, T)to select any attribute node whose name isNand whose type annotation isT, or a subtype ofT -
schema-attribute(N)to select any attribute node whose name isN, that conforms to the schema-defined type for a global attribute declaration namedNin an imported schema -
(T1|T2)to select nodes with node typeT1orT2From XPath 4.0, node types used in steps are extended to allow alternatives, for example,
ancestor::(section|appendix)will select all ancestors namedsectionorappendix. As well as simple names, the options used within the parentheses can include kind tests (such ascomment()andtext()) and wild cards (such as*:codeandns:*).Available since Saxon 13 in XPath 4.0. Available since Saxon 12.0 provided that syntax extensions are enabled.