XQuery is to XML what SQL is to database tables and is being widely used in association with XML for web programming. Prior knowledge of HTML and XML is required to have a firm grip on XQuery. XQuery is mainly designed to query XML data – not just XML files, but anything that can appear as XML, including databases. Good thing about xQuery is that it is supported by all major databases. To put it in simple words, XQuery is a language for finding and extracting elements and attributes from XML documents. Some of the common uses of XQuery could be to extract information to use in a Web Service, to generate summary reports, to transform XML data to XHTML and to search Web documents for relevant information. XQuery is compatible with several W3C standards, such as XML, Namespaces, XSLT, XPath, and XML Schema.
Let’s tackle XQuery with an example whose separate parts we would analyze later. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
In the above example, The doc() function is used to open the “books.xml” file. So, functions are an integral part of an XQuery document.
1 |
doc("books.xml") |
Another important aspect is Path Expressions, which are used to navigate through elements in an XML document. In the above example, the following path expression is used to select all the title elements in the “books.xml” file:
1 |
doc("books.xml")/bookstore/book/title |
The XQuery above will extract the following:
1 2 3 4 |
<title lang="en">Everyday Italian</title> <title lang="en">Harry Potter</title> <title lang="en">XQuery Kick Start</title> <title lang="en">Learning XML</title> |
Finally, Predicates are used to limit the extracted data from XML documents. For example, following predicate is used in the above code:
1 |
doc("books.xml")/bookstore/book[price<30] |
This XQuery will extract the following:
1 2 3 4 5 6 |
<book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> |
Due to its multi dimensional yet simple features, XQuery is increasingly used in web programming. Good thing is, you can learn it within days if you have a prior knowledge of XML. We would discuss some more aspects of XQuery in a future article.