The XPath Data Model

An XPath query operates on a namespace well-formed XML document after it has been parsed into a tree structure. The particular tree model XPath uses divides each XML document into seven kinds of nodes:

root node

The document itself. The root node’s children are the comments and processing instructions in the prolog and epilog and the root element of the document.

element node

An element. Its children are all the child elements, text nodes, comments, and processing instructions the element contains. An element also has namespaces and attributes. However, these are not child nodes.

attribute node

An attribute other than one that declares a namespace

text node

The maximum uninterrupted run of text between tags, comments, and processing instructions. White space is included.

comment node

A comment

processing instruction node

A processing instruction

namespace node

A namespace mapping in scope on an element

The XPath data model does not include entity references, CDATA sections, or the document type declaration. Entity references are resolved into their component text and elements. CDATA sections are treated like any other text, and will be merged with any adjacent text before a text node is formed. Default attributes are applied, but otherwise the document type declaration is not considered.

In the XPath data model each node has a string-value Furthermore, attributes, elements, processing instructions, and namespace nodes have expanded names, which are divided into a local part and a namespace URI. Table 16.1 summarizes XPath’s rules for calculating names and values for its seven node types.

Table 16.1. XPath Expanded Names and String-values

Node typeLocal nameNamespace nameString-value
rootNoneNonethe complete, ordered content of all text nodes in the document; same as the value of the root element of the document
elementThe name of the element, not including any prefix or colonThe namespace URI of the elementThe complete, ordered content of all text node descendants of this element (i.e. the text that’s left after all references are resolved and all other markup is stripped out.)
attributeThe name of the attribute, not including any prefix or colonThe namespace URI of the attributeThe normalized attribute value
textNoneNoneThe complete content of the text node
processing instructionThe target of the processing instructionNoneThe processing instruction data
commentNoneNoneThe text of the comment
namespaceThe prefix for the namespaceNoneThe absolute URI for the namespace

If an XPath function such as local-name() or namespace-uri() attempts to retrieve the value of one of these properties for a node that doesn’t have that property, it returns the empty string.

An diagram and example should help explain this. Consider the simple SOAP response document in Example 16.2.

Example 16.2. A SOAP response document

<?xml version="1.0"?>
<!-- XPath data model example -->
  xmlns:SOAP-ENV="" />
      <Price currency="USD">4.12</Price>

Figure 16.2 is a UML object diagram that identifies the connections between and properties of the different XPath nodes in this document. Solid lines indicate a child relationship. Dashed and dotted lines indicate namespace and attribute connections respectively. Document order runs from top to bottom and left-to-right (although the exact order of namespace nodes and attribute nodes attached to the same element is implementation dependent). One thing to note is that white space is significant in the XPath data model. Line breaks are indicated by \n in this figure.

Figure 16.2. An XPath data model

Copyright 2001, 2002 Elliotte Rusty Haroldelharo@metalab.unc.eduLast Modified June 07, 2002
Up To Cafe con Leche