XML technologies / XPath / XPath 2.0 – Language reference / XPath as a path-description language

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)

XPath is essentially a path-description language whose main purpose is describing parts of XML documents. It is not itself an XML application written in XML, but originally derives from UNIX file syntax. An XPath expression is therefore always string-like, without an internal structure of nested tags, although parentheses can group parts. This allows its use within attribute values, for example in XSLT instructions, or in URI strings.

XSLT as the host language for XPath

For transforming XML documents, XPath 1.0 works with XSLT 1.0. Likewise, XPath 2.0 is always combined with XSLT 2.0, with XSLT serving in both cases as the host language (host language).

Caution – Fixed combinations
These combinations are fixed. It is possible neither to combine XSLT 2.0 with XPath 1.0 nor to combine XSLT 2.0 with XPath 1.0.

XSLT 2.0 and XPath 2.0 are largely backward-compatible with their 1.0 versions. A valid XPath 1.0 expression will therefore generally also be valid in XPath 2.0, usually, but not always, with the same result. This depends on the “compatibility mode” in which XPath 2.0 operates.

The XML document as a tree

XPath expressions selecting parts of an XML document describe target nodes within a tree structure. This resembles the tree structure of the Document Object Model (DOM) familiar from web browsers.

An XML document to be processed must first be converted by the XML parser into an appropriate form, called the XPath Data Model in XPath 2.0. Its tree structure is a directed graph in which every node has an unmistakable identity (unique identity).

The XPath Data Model is based on the XML Information Set, extending it with features needed by XPath, chiefly support for predefined atomic data types from XML Schema.

Extended data model in XPath 2.0
This support for atomic values within XPath 2.0’s data model goes beyond XPath 1.0’s model, which distinguishes only node kinds and uses primitive types only within XPath expressions.

The XPath Data Model tree is drawn downward with its root at the top. The topmost node is therefore always the document node, whose only downward branch points to the root element of the document represented as a tree. The tree can be followed further downward from there, in reading order or document order.

Caution – The document node is not the root element
Do not confuse the data model’s document node with the XML document’s root element: these are different concepts.

The following illustration omits the document node. For simplicity, the graph includes only element nodes; such trees are normally much more complex. Document trees also contain text and comment nodes, whereas attribute and namespace nodes are generally not shown. Reading starts with the root element’s start tag <eins> and continues with the start tag of element <zwei>. This follows the arrows in the graph on the right of the diagram. Within <zwei> are elements <drei> and <vier>. Reading now returns to the end tag of <zwei> and jumps to the start tag of <fuenf>. After reading, in <fuenf>, element <sechs>, it finally returns to the root element’s end tag <eins>, completing the reading process.

Document order

Figure: document order.

Document order is clearer in the tag representation on the left than in the graph. It follows human reading order, the succession of opening and closing tags. This representation is called serialized because its contents form a continuous succession. In the graph, the arrow seems at times to turn upward, but the traversal actually starts at the document node and continually follows the edges counterclockwise from node to node.

Each node combines an element’s start and end tags. Node numbering follows document order, the order in which traversal reaches the nodes. Thus node 5, although higher in the graph than node 3, follows it in document order.

   

back to top