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

Queries

XPath can be thought of as a query language like SQL. Rather than extracting information from a database, however, XPath extracts information from an XML document. An example should help make this more concrete. Consider the simple weather report document in Example 16.1.

Example 16.1 Weather Data in XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<weather time="2002-06-06T15:35:00-05:00">
  <report latitude="41.2° N" longitude="71.6° W">
    <locality>Block Island</locality>
    <temperature units="°C">16</temperature>
    <humidity>88%</humidity>
    <dewpoint units="°C">14</dewpoint>
    <wind>
      <direction>NE</direction>
      <speed units="km/h">16.1</speed>
      <gust units="km/h">31</gust>
    </wind>
    <pressure units="hPa">1014</pressure>
    <condition>overcast</condition>
    <visibility>13 km</visibility>
  </report>
  <report latitude="34.1° N" longitude="118.4° W">
    <locality>Santa Monica</locality>
    <temperature units="°C">19</temperature>
    <humidity>79%</humidity>
    <dewpoint units="°C">16</dewpoint>
    <wind>
      <direction>WSW</direction>
      <speed units="km/h">14.5</speed>
    </wind>
    <pressure units="hPa">1010</pressure>
    <condition>hazy</condition>
    <visibility>5 km</visibility>
  </report>
</weather>

Here are some XPath expressions that identify particular parts of this document:

  • /weather/report is an XPath expression that selects the two report elements.

  • /weather/report[1] is an XPath expression that selects the first report element.

  • /weather/report/temperature is an XPath expression that selects the two temperature elements.

  • /weather/report[locality="Santa Monica"] is an XPath expression that selects the second report element.

  • //report[locality="Block Island"]/attribute::longitude is an XPath expression that selects the longitude attribute of the first report element.

  • /child::weather/child::report/child::wind/child::* is an XPath expression that selects all of the direction, speed, and gust elements.

  • 9 * number(/weather/report[locality="Block Island"]/temperature) div 5 + 32 is an XPath expression that returns the temperature on Block Island in degrees Fahrenheit.

  • /descendant::* is an XPath expression that selects all of the elements in the document.

Like SQL, XPath expressions are used in many different contexts, including the following:

  • Dedicated query tools such as Alex Chaffee's XPath Explorer. Figure 16.1 shows this tool evaluating the expression /weather/report/temperature against Example 16.1.

    Figure 16.1. XPath Explorer

    graphics/16fig01.gif

  • Native XML databases such as the Apache XML Project's XIndice and Software AG's Tamino.

  • As a component of other, broader languages such as XSLT and XQuery.

  • Last but certainly not least, as a search component for your own Java programs that read XML documents.

    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