Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax Free Open Book

Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax

Previous Section Next Section

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="http://schemas.xmlsoap.org/soap/envelope/" />
  <SOAP-ENV:Body>
    <Quote
      xmlns="http://namespaces.cafeconleche.org/xmljava/ch2/">
      <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

graphics/16fig02.gif

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Praise for Elliotte Rusty Harold's 'Processing XML with Java™'
    List of Examples
    List of Figures
    Preface
    Part I: XML
    Part II: SAX
    Part III: DOM
    Part IV: JDOM
    Part V: XPath/XSLT
    Chapter 16. XPath
    Queries
    The XPath Data Model
    Location Paths
    Expressions
    XPath Engines
    DOM Level 3 XPath
    Jaxen
    Summary
    Chapter 17. XSLT
    Part VI: Appendixes


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele