A location step selects a set of nodes (a node-set) relative to the context node.
There are three parts to a location step: an optional axis, a node test, and an optional predicate. The syntax for a location step is the axis name followed by a double colon, then the node test, and finally zero or more predicates, each in square brackets. The most basic form of this syntax is as follows:
axis::nodetest[predicate]
child::
. Furthermore, several axes have shortcut forms; for instance, the ampersand (@
) character is a shortcut for the attribute axis.[
and ]
) in the location step.The node-set selected by a location step results from generating an initial node-set based on the relationship between the axis and node test, and then filtering that initial node-set by each of the predicates in turn.
The initial node-set consists of those nodes that meet the following two criteria:
XPath then uses the first predicate in the location step to filter the initial node-set to generate a new node-set. XPath then uses the second predicate to filter the node-set resulting from the first predicate. This filtering process repeats until XPath has evaluated all the predicates. The node-set that results after applying all the predicates is the node-set selected by the location step.
Note Because the axis affects the evaluation of the expression in each predicate, the semantics of a predicate is defined with respect to the specified axis.
Some sample location steps using the complete syntax are shown in the following table.
Location step | Description |
---|---|
child::*[position()=1] |
Locates the first child node of the context node |
ancestor-or-self::book[@catdate="2000-12-31"] |
Locates all ancestors of any <book> child of the context node, as well as the <book> child itself, as long as the element in question has a catdate attribute with the value "2000-12-31" . |
//self::parent[name()="book"] | descendant::node[name()="author"] |
Locates any node in the document whose parent node is named "book", or any node descended from the context node whose name is "author". |
Axes | Node Tests | Predicates