XPath with XQuery

Besides XSLT, XPath is very popular in W3C's XQuery specification. In fact, XQuery contains XPath as a proper sublanguageall of XPath is contained in XQuery. They are more integrally linked than XPath and XSLT, because you could replace XPath in XSLT with another language and XSLT would still work, but XQuery itself may be thought of as an extension of XPath. XQuery lets you work with XML documents, treating them as databases. You can find information on XQuery at http://www.w3.org/XML/Query.

A lot of XQuery's data-accessing power is considered very important by W3C, which is why much of it is being incorporated into XPath 2.0. (We'll see how XPath and XQuery work together in Chapter 6.) Here's an example where we'll take a look at all books published by Sams since 1990, as stored in a database named books.xml . Here's what the XQuery query might look like:

 
 <books> {     for $book in doc("http://www.starpowder.com/books.xml")     where $book/publisher = "Sams" and $book/@year > 1990     return         <book year={$book/@year}>         {$book/title}         </book> } </books> 

In this case, $book stands for the root node of books.xml , and you can create XPath expressions using $book such as $book/publisher , which stands for the value of child <publisher> elements, or $book/@year , which stands for the value of year attributes. Here's what the results of this query might look like:

 
 <books>     <book year="1990">         <title>Title 1</title>     </book>     <book year="1991">         <title>Title 2</title>     </book>         .         .         . </books> 


XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

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