Data Access with XQuery
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.

View Large Image

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.

return $x/title

When the following XQuery statement:

for $x in document("books.xml")//book where $x/author = "Joe Smith" return $x/title

queries this document:

<inventory>
   <book category="Fiction">
     <title>Joy of Integration</title>
     <author>Joe Smith</author>
   </book>
   <book category="Non-Fiction">
     <title>Integration for Dummies</title>
     <author>John Doe</author>
   </book>
</inventory>

"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.

SOA Design Patterns by Thomas Erl
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.
Web Service Contract Design & Versioning for SOA by Thomas Erl, Anish Karmarkar, Priscilla Walmsley, Hugo Haas, Umit Yalcinalp, Canyang Kevin Liu, David Orchard, Andre Tost, James Pasley
SOA Principles of Service Design by Thomas Erl
Service-Oriented Architecture: A Field Guide to Integrating XML and Web Services by Thomas Erl
Service-Oriented Infrastructure:On-Premise and in the Cloud by Raj Balasubramanian, Benjamin Carlyle, Thomas Erl, Cesare Pautasso
Next Generation SOA:A Real-World Guide to Modern Service-Oriented Computing by Pethuru Cheliah, Thomas Erl, Clive Gee, Robert Laird, Berthold Maier, Hajo Normann, Leo Shuster, Bernd Trops, Clemens Utschig, Torsten Winterberg
SOA with .NET & Windows Azure: Realizing Service-Orientation with the Microsoft Platform by David Chou, John deVadoss, Thomas Erl, Nitin Gandhi, Hanu Kommalapati, Brian Loesgen, Christoph Schittko, Herbjorn Wilhelmsen, Mickey Williams
SOA Governance:
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
SOA with Java by Raj Balasubramanian, David Chou, Thomas Erl, Thomas Plunkett, Satadru Roy, Philip Thomas, Andre Tost
Modern SOA Methodology: Methods for Applying Service-Orientation On-Premise & in the Cloud by Raj Balasubramanian, David Chou, Thomas Erl, Thomas Plunkett, Satadru Roy, Philip Thomas, Andre Tost
Cloud Computing: Concepts, Technology & Architecture by Thomas Erl, Zaigham Mahmood, Ricardo Puttini
Cloud Computing Design Patterns by Thomas Erl, Amin Naserpour

For more information about these books, visit: www.servicetechbooks.com


Arcitura Education Inc.
Arcitura Education Inc. is a leading global provider of progressive, vendor-neutral training and certification programs, providing industry-recognized certification programs for a range of certifications.
For more information:
www.arcitura.com
SOA Certified Professional (SOACP)
The books in this series are part of the official curriculum for the SOA Certified Professional program.
For more information:
www.soaschool.com
Cloud Certified Professional (CCP)
The books in this series are part of the official curriculum for the Cloud Certified Professional program.
For more information:
www.cloudschool.com
Big Data Science Certified Professional (BDSCP)
The books in this series are part of the official curriculum for the Big Data Science Certified Professional program.
For more information:
www.bigdatascienceschool.com/