Syntax of path expressions

Each step of a path expression is either an axis step or a filter expression. An axis step returns a sequence of nodes that are reachable from the context node via a specified axis. A filter expression consists of a primary expression that is followed by zero or more predicates.

Read syntax diagramSkip visual syntax diagram//// or //axis stepfilter expression
axis step
Read syntax diagramSkip visual syntax diagramaxis::node-test[ PredicateExpression]
filter expression
Read syntax diagramSkip visual syntax diagramPrimaryExpression[ PredicateExpression]
/
An initial slash character (/) indicates that the path begins at the root node, which must be a document node, of the tree that contains the context node. Slash characters within a path expression separate steps.
//
An initial double slash character (//) indicates that the path begins with an initial node sequence that consists of the root node, which must be a document node, of the tree that contains the context node, plus all of the descendants of the root node. To understand the meaning of a double slash character between steps, see the topic about abbreviated syntax.
axis
A direction of movement through an XML document or fragment. The list of supported axes includes child, descendant, attribute, self, descendant-or-self, and parent. Some of these axes can be represented by using an abbreviated syntax.
node-test
A condition that must be true for each node that is selected by an axis step. This test can be either a name test that selects nodes based on the name of the node or a kind test that selects nodes based on the kind of node.
PrimaryExpression
A primary expression.
PredicateExpression
An expression that determines whether items of the sequence are retained or discarded.

Examples

The following example shows an axis step that includes two predicates. This step selects all the employee children of the context node that have both a secretary child element and an assistant child element:
child::employee[secretary][assistant]
The following example uses a filter expression as a step in a path expression. The expression returns every chapter or appendix that contains more than one footnote within a given book:
$book/(chapter | appendix)[fn:count(footnote)> 1]