by Thomas Erl
The XQuery specification establishes a comprehensive data query language, designed specifically for XML documents. XQuery is aligned with and overlaps considerably with release 2.0 of the XPath specification. XQuery uses the XPath language to define data source addressing, and even adds some new XPath extensions.
XQuery statements can be isolated into independent functions. Functions can then be logically organized into modules.
A module can contain a collection XQuery functions that can be imported into other modules.
XQuery expressions can be embedded within an XML document (in which case they are enclosed within braces).
XQuery is often compared to the Structured Query Language (SQL). In fact, some XQuery features are even derived from traditional SQL statements. A significant characteristic of XQuery is that it enables you to perform a search across multiple XML repositories with a single query statement. This establishes a data source independence that broadens the application of XQuery beyond that of SQL.
Depending on the nature of the data you are searching, and the type of query expression you need to build, you can choose from one of several available expression formats. These include:
• FLWR (pronounced "flower"), which provides a series of keywords that are comparable to those used by SQL.
• Path expressions, which allow you to access elements and attributes of an input document.
• Element constructors that enable the dynamic creation of new XML elements and values.
• Conditional expressions that can add complex logic to query statements using the well-known if-then-else construct.
• Quantified expressions that allow for the inclusion of quantifying logic. Using keywords, such as some and every, submitted test expressions result in either "falseor "true".
For our example we"ll be building a FLWR query expression using the for, where and return keywords. The for clause below provides a variable that is bound to the book element of the books.xml document. The values that will be output are defined by the subsequent XQuery clauses.
for $x in document("books.xml")//book
The where fragment is very similar to the SQL WHERE clause, in that it defines the search criteria. In this case, we are searching for all books written by "Joe Smith".
where $x/author = "Joe Smith"
In order to define what data from the queried XML tree we actually want returned, XQuery provides the return keyword (much like an SQL SELECT statement). In response to our query, we"d like to get the titles written by Joe Smith.
When the following XQuery statement:
for $x in document("books.xml")//book where $x/author = "Joe Smith" return $x/title
queries this document:
<title>Joy of Integration</title>
<title>Integration for Dummies</title>
"it returns the following result:
<title>Joy of Integration</title>
With numerous types of expressions at its disposal, the XQuery specification can be used to create complex query logic. Key features to look out for include support for XSD data types, XPath functions, SQL aggregation functions, and many other SQL features, such as grouping, joins and sorting.
- Inside XML Schemas
- SOAP in a Nutshell
- Transforming Data with XSLT
- Understanding DTDs
- Why SAX is Good for DOM
- What You Should Know about XPath
- An XHTML Primer
- XLink - Inside and Out
- Data Access with XQuery
- XSL versus CSS
- Another Introduction to XML
- Unifying Corporate Data & Documents
- Replacing HTML Documents with XML
- Meta-Enable Your Enterprise
- The XML Data Custodian
- Integrating XML into the Enterprise
- The Wireless Enterprise
Foreword by Grady Booch
With contributions from David Chappell, Jason Hogg, Anish Karmarkar, Mark Little, David Orchard, Satadru Roy, Thomas Rischbeck, Arnaud Simon, Clemens Utschig, Dennis Wisnosky, and others.
Governing Shared Services On-Premise & in the Cloud by Stephen Bennett, Thomas Erl, Clive Gee, Anne Thomas Manes, Robert Schneider, Leo Shuster, Andre Tost, Chris Venable
For more information about these books, visit: www.servicetechbooks.com