XML technologies / XPath / XPath 2.0 – Language reference / Path expressions: fundamentals
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)
A basic XPath function locates the desired nodes within a tree and selects them as a sequence. A path expression returns no atomic values, so its result is always a pure node sequence, or an empty sequence if no node matches. Similarly, XPath 1.0 returns an empty node-set when nothing matches. The expressions used for this are called path expressions (path expressions).
Path expressions in XPath 1.0 and XPath 2.0
In XPath 1.0, a path expression generally yields a node-set. Although node-sets are unordered by definition, they are processed in a specified order corresponding to document order in the XML document being processed.
In XPath 2.0, a path expression yields a sequence, which is always ordered. Its processing is thus fundamentally independent of the represented nodes’ document order and depends only on sequence order.
To ensure backward compatibility between XPath 2.0 and XPath 1.0 path expressions, however, nodes in the sequence must also follow document order, and duplicate nodes are removed.
Identical path expressions yield identical results in XPath 1.0 and 2.0
In this sense, a node sequence produced by an XPath 2.0 path expression corresponds to the node-set produced by the same expression in XPath 1.0, provided the expression can also be written there.
Path expressions in general
A path expression consists of at least one step (Location Step). Multiple steps are separated by path operator “/”. Each step selects nodes along an axis, one of the possible directions of movement within a tree.
A step expression is itself divided into an axis name and another subexpression specifying the desired nodes. This node test can be refined by additional filter conditions (Predicates):
achsenbezeichner::nodetest[predicate]
Before examining axes and then steps as a whole, we first consider the basic principle of path expressions.
Relative and absolute path expressions
Path expressions are relative or absolute depending on where they begin.
A relative path expression always starts at the current context node.
Its result sequence therefore depends on context. Reevaluating it with a changed context node produces a new result sequence. Here is a schematic example of a relative path expression:
step_1/step_2/step_3An absolute path expression instead starts at the document node.
Its result sequence is independent of context and the current node’s position in the document tree. Reevaluating it with a changed context node always produces the same sequence. An absolute path expression begins with the path operator itself:
/step_1/step_2/step_3
The double slash in path expressions
Within a path expression, the double slash is an expression in its own right, not two successive single path operators. It approximately corresponds to the descendant-or-self axis described below.
A special feature: steps following a double slash “//” can start at any depth in the document tree.
The double slash can appear at the start of a path expression
//step_1/step_2/step_3
or between two steps:
step_1/step_2//step_3
At the start of a path expression it marks an absolute path, just like an initial single slash.
Thus, //step_1 forms a sequence of all nodes below the document node satisfying step_1. Matching nodes can be at any depth in the document tree, including the document node itself if it matches the step’s node test.
The somewhat more complex expression step_1//step_2 first requires, starting at the context node, matching step_1. This forms an initial node sequence. All document branches descending from its nodes are then searched for nodes satisfying step_2. Each node in the initial sequence is thus treated as the root of a branch descending from it.
Several successive nodes per branch can enter the result if they match the node test, since the entire branch is checked. The nodes of the initial sequence assembled by step_1, the roots of the checked branches, are also considered if they likewise satisfy the node test of step2. All successfully tested nodes together form the result sequence.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn