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 of the child elements, text nodes, comments, and processing instructions that 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.

Processing Instruction Node

A processing instruction.

Comment Node

A comment.

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 the same as 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 Type Local Name Namespace Name String Value
Root None None The complete, ordered content of all text nodes in the document; same as the value of the root element of the document
Element The name of the element, not including any prefix or colon The namespace URI of the element The 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)
Attribute The name of the attribute, not including any prefix or colon The namespace URI of the attribute The normalized attribute value
Text None None The complete content of the text node
Processing instruction The target of the processing instruction None The processing instruction data
Comment None None The text of the comment
Namespace The prefix for the namespace None The 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, then it returns the empty string. An example and a diagram 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 --> <SOAP-ENV:Envelope   xmlns:SOAP-ENV="" />   <SOAP-ENV:Body>     <Quote       xmlns="">       <Price currency="USD">4.12</Price>     </Quote>   </SOAP-ENV:Body> </SOAP-ENV:Envelope> 

Figure 16.2 is a UML object diagram that identifies the properties of the different XPath nodes in this document and the connections between them. 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). Line breaks are indicated by \n in this figure. Note that white space is significant in the XPath data model.

Figure 16.2. An XPath Data Model


Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191 © 2008-2017.
If you may any questions please contact us: