Compartilhar via


Location Steps

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]
  • axis
    Specifies the tree relationship between the context node and the nodes to be selected by the location step. In other words, the axis indicates the general direction that the location step proceeds from the context node. In a location step, the axis is optional. If omitted, the axis defaults to child::. Furthermore, several axes have shortcut forms; for instance, the ampersand (@) character is a shortcut for the attribute axis.
  • nodetest
    Specifies the node type or expanded name of the nodes to be initially selected by the location step. Basically, the node test indicates which node(s), from among all nodes on the indicated axis, to consider as candidates, that is, potential, matches for the location step.
  • predicate
    Uses an XPath expression (condition to be met) to further refine the set of nodes selected by the location step. The predicate is a filter, specifying a selection criterion for further refining the list of candidate nodes. The predicate is optional. If there is no predicate, there are no square brackets ([ and ]) in the location step.

Selecting Nodes

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:

  • The nodes have the relationship to the context node specified by the axis.

  • The nodes have the node type and expanded name specified by the node test.

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.

NoteNote

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".

//parent::node()[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".

See Also

Reference

Axes
Node Tests
Predicates