XML Query (XQUERY) is a computer language that provides a way to read, extract and transform XML documents. XQuery includes language constructs to read XML, select and order specific values and return the result in the form of another XML document, for instance an xhtml web page.
XSLT also provides these services but many people feel that XSLT is very difficult to learn. People with an SQL background experience difficulty in understanding the unfamiliar XML syntax of XSLT.
XQuery depends on XPath. XPath is an expression language that allows the processing of XML documents by providing a representation in the form of a tree of XML elements.
⇓ Click the down arrow for more detail. ⇓
⇑ Click the up arrow to go back to the top of the page. ⇑
XPath Examples/bookstore/book/title
/bookstore/book[1]/title
/bookstore/book[price>35]/price
XQuery ExampleFor each book in an XML document, list only those books published by Addison-Wesley after 1991. Include the book's year and title in the output:
for $b in document("www.bn.com/bib.xml")/bib/book
where $b/publisher="Addison-Wesley" and $b/@year>1991
return <book year="{$b/@year}">{$b/title}</book>
In the example,
$b
is a temporary variable that represents the book elements in the XML document. We use XPath statements to refer to other elements as in
$b/publisher
. The result of the XQuery (specified in the
return
block) is a document with book elements that might look like:
<book year="1992">2010 The Odyssey Continues</book>
<book year="1994">Radiohead - The band that saved Rock</book>
FLWOR ExpressionsXQuery statments are called FLWOR expressions:
⃘
for - specifies what items in the sequence you want to select
⃘
let - used to create temporary names used in the return
⃘
where - limit items returned
⃘
order - specify the sort order of the results
⃘
return - specify the structure of the data returned