XQuery

Overview

* Used to query XML data
* XQuery to XML is Like SQL is to database tables
* Built on XPath expression
* Uses FLWOR (“flower”) expression: for, let, where, order by, return
* Is a W3C recommendation (2007)
* Compatible with several W3C standards
– XML
– XML Schema
– Namespaces
– XPath
– XSLT

Features

* Logical/physical data independence
* Declarative
* High level
* Side-effect free
* Strongly typed

Pros

* Easier to learn. Language more concise (less verbose).

Cons

* Can not update XML documents or databases (supported in XQuery Update Facility)
* Lacks full text search capability (supported in XQuery Full-Text)
* No dynamic binding or ploymorphism

Compare to Others

XQuery 1.0 vs XPath 2.0

* XQuery inclues XPath 2.0 as a sublanguage
* Share same
– data model
– type system
– function libraries

XQuery vs XSLT 2.0

* Included in XSLT but missing in XQuery
– grouping
– formatting (number, date)
– greater control over XML namespaces
* XSLT itself is XML so can be manipulated by XML tools (e.g. dynamically generated)
*XQuery is NOT XML itself (XQueryX is)

Shared

* Both include XPath 2.0 as a sublanguage
* Both share same
– data model
– type system
– function libraries

XQuery Processors

* Qizx/open. Also this post.
* Saxon
* XQJ (XQuery for Java)

XQuery Functions

Sample xml doc: books.xml

<?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>

Open xml file

doc("books.xml")

Path expressions

doc("books.xml")/bookstore/book/titile

Predicates

doc("books.xml")/bookstore/book[price<30]

FLWOR

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

References

* W3C XQuery
* XQuery Update Facility 1.0
* Wikipedia: XQuery
* Comparing XSLT and XQuery By J. David Eisenberg

This entry was posted in xml, xquery. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.