Some Illustrative XQuery Use Cases

In this section, we will look at how XQuery code for some typical XQuery use cases might be written. In the space available, the examples only serve to illustrate the type of queries that XQuery will be able to execute. The absence of a particular type of query in this section shouldn't lead you to think that XQuery lacks any particular functionality. Check the latest XQuery specification documents to establish the latest position.

One XQuery early implementation from X-Hive.com is available for use online and makes use of examples contained in the W3C XQuery Use Cases draft. To view the XQuery demo, visit http://www.x-hive.com/xquery.

Thus, you can work through the examples shown in this section online at X-Hive.com. Naturally, you can also create your own queries to learn more about XQuery.

The XML document we will use for our data is found in the XML Query Use Cases Working Draft, http://www.w3.org/TR/2001/WD-xmlquery-use-cases-20011220, and is shown in Listing 20.10. This document is extremely useful as you work to get up to speed with XQuery.

Listing 20.10. The W3C XML Use Cases Source XML Code (bib.xml)
<bib>
 <book year="1994">
  <title>
   TCP/IP Illustrated
  </title>
  <author>
   <last>Stevens</last>
   <first>W.</first>
  </author>
  <publisher>Addison-Wesley</publisher>
  <price> 65.95</price>
 </book>
 <book year="1992">
  <title>
   Advanced Programming in the Unix environment
  </title>
  <author>
   <last>Stevens</last>
   <first>W.</first>
  </author>
  <publisher>Addison-Wesley</publisher>
  <price>65.95</price>
 </book>
 <book year="2000">
  <title>
   Data on the Web
  </title>
  <author>
   <last>Abiteboul</last>
   <first>Serge</first>
  </author>
  <author>
   <last>Buneman</last>
   <first>Peter</first>
  </author>
  <author>
   <last>Suciu</last>
   <first>Dan</first>
  </author>
  <publisher>Morgan Kaufmann Publishers</publisher>
  <price> 39.95</price>
 </book>
 <book year="1999">
  <title>
   The Economics of Technology and Content for Digital TV
  </title>
  <editor>
   <last>Gerbarg</last>
   <first>Darcy</first>
   <affiliation>CITI</affiliation>
  </editor>
  <publisher>Kluwer Academic Publishers</publisher>
  <price>129.95</price>
 </book>
</bib>

Listing 20.10 describes a number of books. The document element is a bib element. The structure of most book elements is the same, but notice that the final book has an editor element rather than one or more author elements.

A Simple Book Listing

Let's create a simple query that will retrieve the content of all the publisher and title elements. The bib and book elements are created literally, not retrieved from the document being queried.

Note

All queries in this section are based on the example XML document from the XQuery Use Cases document. These have been modified to be able to run on the X-Hive.com online demo. If you want to run these with other XQuery implementations of the Use Cases, you will likely have to alter paths to files, for example.


Note that in the third line of the query, the path to the bib.xml file is shown as the argument to the document() function. The path used is the path to allow you to use the data on the live X-Hive demo. To run the queries in another setting, you would have to appropriately modify the path to bib.xml.

Listing 20.11. An XQuery Query to Retrieve the Content of Publisher and Title Elements (Query01.txt)
<bib>
 {
  for $b in document("/XQuery/docs/XMP/bib.xml")/bib/book
  return
    <book>
     { $b/title }
     { $b/publisher }
    </book>
 }
</bib>

This is a form of the FLWR expression that contains FOR and RETURN statements.

The FOR statement sets the variable $b to correspond to a book element node in bib.xml. The RETURN statement creates a new literal book element for each node in $b and outputs a title element node and a publisher element node that are child nodes of the nodes represented by $b. Finally, an end tag for the book element is output.

Note

Notice that the keywords FOR, LET, WHERE, and RETURN are not case sensitive. In this respect, XQuery resembles SQL where keywords are not case sensitive.


The result from the query is shown in Listing 20.12.

Listing 20.12. The Result from the Query Shown in Listing 20.11 (Query01Out.xml)
<bib>
 <book>
  <title>
   TCP/IP Illustrated
  </title>
  <publisher>Addison-Wesley</publisher>
 </book>
 <book>
  <title>
   Advanced Programming in the Unix environment
  </title>
  <publisher>Addison-Wesley</publisher>
 </book>
 <book>
 <title>
  Data on the Web
 </title>
 <publisher>Morgan Kaufmann Publishers</publisher>
 </book>
 <book>
  <title>
   The Economics of Technology and Content for Digital TV
  </title>
  <publisher>Kluwer Academic Publishers</publisher>
 </book>
</bib>

A Listing of Recent Books

Suppose that you want to find books published after the year 1995 and create a new XML document whose document element is a name of your choosing, such as a RecentBooks element.

Listing 20.13 shows a query to create a literal RecentBooks element as the document element and to select books that have a year attribute whose value is greater than 1995. We use a WHERE statement to compare the year of publication with 1995.

Listing 20.13. An XQuery Query to Retrieve Books Published Since 1995 (Query02.txt)
<RecentBooks>
 {
  for $b in document("/XQuery/docs/XMP/bib.xml")/bib/book
  where $b/@year > 1995
  return
    <book year={ $b/@year }>
     { $b/title }
     { $b/publisher }
    </book>
 }
</RecentBooks>

The XML document created by the query in Listing 20.13 is shown in Listing 20.14.

Listing 20.14. Result of the XQuery Query to Select Books Published After 1995 (Query02Out.xml)
<RecentBooks>
 <book year="2000">
  <title>
   Data on the Web
  </title>
  <publisher>Morgan Kaufmann Publishers</publisher>
 </book>
 <book year="1999">
  <title>
   The Economics of Technology and Content for Digital TV
  </title>
  <publisher>Kluwer Academic Publishers</publisher>
 </book>
</RecentBooks>

Sorting the Query Results by Publisher

We can modify Listing 20.13 to sort the output by the value of the publisher element in the input document. Listing 20.15 shows how this can be done.

Listing 20.15. Query Selecting Books Published After 1995, Sorted Alphabetically by Publisher (Query03.txt)
<RecentBooks>
 {
  for $b in document("/XQuery/docs/XMP/bib.xml")/bib/book
  where $b/@year > 1995
  return
    <book year={ $b/@year }>
     { $b/title }
     { $b/publisher }
    </book>
   sortby(publisher/text() ascending)
 }
</RecentBooks>

The sortby() function sorts the output by the text value of the publisher element node.

The output, sorted alphabetically by publisher, is shown in Listing 20.16.

Listing 20.16. Books Published After 1995 Alphabetically Sorted by Publisher (Query03Out.xml)
<RecentBooks>
  <book year=" year="1999"">
    <title>The Economics of Technology and Content for Digital TV</title>
    <publisher>Kluwer Academic Publishers</publisher>
  </book>
  <book year=" year="2000"">
    <title>Data on the Web</title>
    <publisher>Morgan Kaufmann Publishers</publisher>
  </book>
</RecentBooks>

There is much, much more to XQuery than has been introduced in this chapter. It will, after the specification has been finalized, be a key area of knowledge for many who work with XML. If this is a topic that interests you, be sure to follow the development of the XQuery specification in all its facets as defined in the various W3C working drafts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.69.53