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.



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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net