Location paths are a particular kind of expression that yields a node-set. Because they are so important, we will discuss them first, before general XPath expressions. Two kinds of location paths exist: relative and absolute.
Relative Location Paths
The evaluation of a relative location path occurs relative to some current context node. It consists of one or more location steps. If multiple steps are present, they are separated by slashes ("/").
The evaluation of these location steps goes from left to right, with the information being passed along consisting of a selected node-set. The node-set fed into the first or leftmost (and possibly only) location step consists of just the context node. The evaluation of a particular node step occurs for each node in the input node-set that the location steps to its left produce.
The node step can eliminate a node, produce a set of nodes, or produce a single node, which can be the same as or different from its input. The results obtained from evaluating a location step for each input node are merged into a node-set. This set then serves as the input for the next location step or as the value of the location path if no more location steps appear to the right. (See the upper part of Figure 6-3.)
Figure 6-3. Location paths and steps
Absolute Location Paths
An absolute location path begins with a forward slash character ("/"), which represents the root node of the document containing the context node. If the absolute location path consists of just a slash, it selects only this root node. If a relative location path follows the slash, the path name evaluates to that relative path location with the initial input of this root node.
Abbreviated Location Paths
For convenience, abbreviations are available for commonly used parts of absolute and relative locations paths, as described in Section 6.3.5.
6.3.1 Location Steps
Every location step consists of three parts, as shown in the lower part of Figure 6-3:
You separate the axis from the node test with a double colon ("::"). Both are always present in a location step, although some abbreviations can substitute for the axis and node test or just for the axis (see Table 6-3). Predicates are optional.
The axis portion of a location step takes each input node to that step and coverts it to zero or more nodes with a particular data model relationship to the input node. Table 6-1 lists the 13 axes defined by XPath.
6.3.3 Node Tests
The node test portion of a location step applies a test to the name or type of each node in the node-set produced by the axis. Table 6-2 lists the node tests found in XPath. Several are defined in terms of the "principal node type" of the axis. That type is "element" except for the "attribute::" and "namespace::" axes, where the principal node types are "attribute" and "namespace," respectively.
is the set of all the text nodes in the document after the context node ignoring descendants. By comparison,
is the set of all elements with name "foo" in the namespace bound to the prefix "n1" in the document containing the context node.
One or more predicates, each inside square brackets, can optionally appear in a location step after the node test. Each predicate is evaluated, and nodes are selected only if all predicates present evaluate to "true." However, the truth values of numeric predicate expressions are specially determined. In particular, if the "position" of the node (defined below) matches the numeric value of the predicate, the predicate is considered "true." If they are not equal, the predicate is considered "false" and the node is not selected.
"Position" is the location at which a node appears in an axis, starting at 1, in document order for a forward axis, and in reverse document order for a reverse axis.
Except for the "self::" axis, where it doesn't matter, only the ancestor,
Except for the "self::" axis, where it doesn't matter, only the ancestor, ancestor-or-self, preceding, and preceding-sibling axes use a reverse document order.
have the same effect as predicates namely, selecting only the seventh position node in the location step axis.
selects all elements whose local names start with "de" in the document containing the context node.
6.3.5 Abbreviated Notation
Table 6-3 lists the abbreviations available in XPath.
The shortest way in XPath to select all nodes in a node-set is as follows:
( //. | //@* | //namespace::* )
XPath does not consider attributes and namespace nodes to be descendants of an element. Thus the attributes and namespaces must be gathered separately. This expression occurs frequently in connection with XML Security, particularly during canonicalization.