6.3 Location Paths


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

graphics/06fig03.gif

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:

An axis, which specifies the data model relationship between the input node and the nodes that are subject to the node test. The simplest axis, "self," passes the input node to the node test.

The node test can filter the nodes selected by the axis to nodes of a certain type or name.

Predicates are enclosed in square brackets ("[ ]") and apply further tests that can eliminate nodes.

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.

6.3.2 Axes

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.

Table 6-1. XPath Axes
Axis Description
ancestor:: The node-set consisting of the parent of the context node, the parent of that node, and so on. Always includes the root node unless the context node is the root node, in which case the result is the empty node-set.
ancestor-or-self:: Same as the "ancestor::" axis except that it also includes the context node.
attribute:: The node-set of the attributes of the context node. It produces the empty set if the context node is not an element, or it is an element with no attributes.
child:: The children of the context node. Attributes and namespace nodes are not considered children.
descendant:: The node-set consisting of the children of the context node, the children of the children, and so on. Attributes and namespace nodes are not considered children.
descendant-or-self:: Same as the "descendant::" axis except that it also includes the context node.
following:: The set of all nodes that appear after the context node in document order, excluding attribute nodes, namespace nodes, and descendant nodes.
following-sibling:: The node-set of the children of the context node's parent that appear after the context node in document order. However, If the context node is an attribute or namespace node, the "following-sibling::" axis is empty.
namespace:: The node-set of the namespace declarations for which the context node is the parent. It consists of the empty set if the context node is not an element or if it is an element with no namespace declarations.
parent:: The node-set consisting of the one node that is the parent of the context node, unless the context node is the root; in that case, it produces the empty node-set.
preceding:: The set of all nodes that appear before the context node in document order, excluding attribute nodes, namespace nodes, and descendant nodes.
preceding-sibling:: The node-set of the children of the context node's parent, which appear before the context node in document order. If the context node is an attribute or namespace node, the "preceding-sibling::" axis is empty.
self:: The identity axis. It produces the node-set consisting of the context node.

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.

For example,

 following::text() 

is the set of all the text nodes in the document after the context node ignoring descendants. By comparison,

 /descendant::n1:foo 

is the set of all elements with name "foo" in the namespace bound to the prefix "n1" in the document containing the context node.

6.3.4 Predicates

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.

Table 6-2. XPath Node Tests
Node Test Description
::node() Does nothing. It selects all nodes of any type from the axis output.
::* Selects all nodes with the same type as the principal node type of the axis.
::text() Selects only text nodes.
::comment() Selects only comment nodes.
::processing-instruction() Selects only processing instruction nodes.
::processing-instruction(Literal) Selects only processing instruction nodes with a target equal to Literal.
::QName

Selects nodes only if their expanded names have the same local part and namespace URI as the expansion of QName.

A QName is a possibly namespace-qualified name. It is interpreted as an expanded-name, with the prefix being expanded based on the expression context in the same way as attribute names are expanded. That is, if no namespace prefix exists, the namespace URI is null. An error occurs if QName uses an unbound namespace prefix.

::NCName:* Selects nodes only if their expanded names have a namespace URI matching that to which the NCName prefix is bound. An error occurs if it is not bound to a namespace. The local part of the name can be anything legal for an XML local name.

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

  • A forward axis can contain nodes only at or after the context node in document order.

  • A reverse axis can contain nodes only at or before the context node in document order.

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.

Thus

 [position()=7] 

and

 [3+4] 

have the same effect as predicates namely, selecting only the seventh position node in the location step axis.

 /descendant::*[starts-with(local-name(),"de")] 

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.



Secure XML(c) The New Syntax for Signatures and Encryption
Secure XML: The New Syntax for Signatures and Encryption
ISBN: 0201756056
EAN: 2147483647
Year: 2005
Pages: 186

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net