XQuery: query collections of XML data

15 July, 2008 at 22:15 2 comments

XQuery imageXQuery is a query language (with some programming language features) that is designed to query collections of XML data. It is semantically similar to SQL.

XQuery 1.0 was developed by the XML Query working group of the W3C. The work was closely coordinated with the development of XSLT 2.0 by the XSL Working Group; the two groups shared responsibility for XPath 2.0, which is a subset of XQuery 1.0. XQuery 1.0 became a W3C Recommendation on January 23, 2007.

“The mission of the XML Query project is to provide flexible query facilities to extract data from real and virtual documents on the World Wide Web, therefore finally providing the needed interaction between the Web world and the database world. Ultimately, collections of XML files will be accessed like databases”.

XQuery can be used to:

  • Extract information to use in a Web Service
  • Generate summary reports
  • Transform XML data to XHTML
  • Search Web documents for relevant information

XQueryX is a near one-to-one mapping into XML of the formal grammar for the language. Given the complexity of the grammar, this makes XQueryX highly verbose, to the degree that it’s nearly impossible for humans to read. Happily, machines — which are the intended recipients of the language — don’t complain about such things. Listings 1 and 2 provide a comparison of a simple query expressed first in standard XQuery syntax and then in its XQueryX counterpart. Note the significant steroid-like bulk-up factor.
Listing 1. A simple query in standard syntax

				<bib>
 {
  for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
  where $b/publisher = "Addison-Wesley" and $b/@year > 1991
  return
    <book year="{ $b/@year }">
     { $b/title }
    </book>
 }
</bib>

Listing 2 shows the XQueryX equivalent. I’ve omitted about three-quarters of the listing due to its length. The full listing, lifted directly from the XQueryX working draft, runs to 132 lines:
Listing 2. The Listing 1 query in XQueryX format (snippet)

				<?xml version="1.0"?>
<xqx:module xmlns:xqx="http://www.w3.org/2005/XQueryX" ... >
  <xqx:mainModule>
    <xqx:queryBody>
      <xqx:elementConstructor>
        <xqx:tagName>bib</xqx:tagName>
        <xqx:elementContent>
          <xqx:flworExpr>
            <xqx:forClause>
              <xqx:forClauseItem>
                <xqx:typedVariableBinding>
                  <xqx:varName>b</xqx:varName>
                </xqx:typedVariableBinding>
                <xqx:forExpr>
                  <xqx:pathExpr>
                    <xqx:argExpr>
                      <xqx:functionCallExpr>
                        <xqx:functionName>doc</xqx:functionName>
                        <xqx:arguments>
                          <xqx:stringConstantExpr>
                            <xqx:value>http://bstore1.example.com/bib.xml</xqx:value>
                          </xqx:stringConstantExpr>
                        </xqx:arguments>
                      </xqx:functionCallExpr>
                    </xqx:argExpr>
                    <xqx:stepExpr>
                      <xqx:xpathAxis>child</xqx:xpathAxis>
                      <xqx:nameTest>bib</xqx:nameTest>
                    </xqx:stepExpr>
                    <xqx:stepExpr>
                      <xqx:xpathAxis>child</xqx:xpathAxis>
                      <xqx:nameTest>book</xqx:nameTest>
                    </xqx:stepExpr>
                  </xqx:pathExpr>
                </xqx:forExpr>
              </xqx:forClauseItem>
            </xqx:forClause>
			...

How to Select Nodes From “books.xml”?

Functions

XQuery uses functions to extract data from XML documents.

The doc() function is used to open the “books.xml” file:

doc("books.xml")

Path Expressions

XQuery uses path expressions to navigate through elements in an XML document.

The following path expression is used to select all the title elements in the “books.xml” file:

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

(/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element)

The XQuery above will extract the following:

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

Predicates

XQuery uses predicates to limit the extracted data from XML documents.

The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30:

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

The XQuery above will extract the following:

<book category=”CHILDREN”> <title lang=”en”>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>

Path Expressions and Relationship to XPath

XQuery borrows path expressions from XPath. XQuery can be viewed as a generalization of XPath. Except for some obscure forms (mostly unusual “axis specifiers”), all XPath expressions are also XQuery expressions. For this reason the XPath specification is also being revised by the XQuery committee, with the plan that XQuery 1.0 and XPath 2.0 will be released about the same time. Example

XQuery Tools support

db2expln

The db2expln tool describes the access plan selected for SQL and XQuery statements. It can be used to obtain a quick explanation of the chosen access plan when explain data was not captured. For static SQL and XQuery statements, db2expln examines the packages stored in the system catalog tables. For dynamic SQL and XQuery statements, db2expln examines the sections in the query cache.

AltovaXML 2006

AltovaXML is a FREE XML standards processor that includes the Altova XSLT 1.0 engine, XSLT 2.0 engine, XQuery engine, and XML validating parser. Now developers can use the same engines found in Altova XMLSpy 2006 in their own application, free of charge.

XQuery Tips and Tricks

XQuery Tutorial

XQuery By Example

[wikipedia.org]

[http://www.w3schools.co]

[http://www.ibm.com/developerworks/xml/library/x-xquery.html]


Bookmark and Share

Advertisements

Entry filed under: SQL, XML, XPath, XQuery. Tags: , , , .

Java Annotations: Concepts, Methods and Examples Agile Experiences: Software Engineering Radio interview Jens Coldewey

2 Comments Add your own

  • […] enflicted.net wrote an interesting post today onHere’s a quick excerpt XQuery is a query language (with some programming language features) that is designed to query collections of XML data. It is semantically similar to SQL. XQuery 1.0 was developed by the XML Query working group of the W3C. The work was closely coordinated with the development of XSLT 2.0 by the XSL Working Group; the two groups shared responsibility for XPath 2.0, which is a subset of XQuery 1.0. XQuery 1.0 became a W3C Recommendation on January 23, 2007. “The mission of the XML Query projec […]

    Reply
  • 2. cheat codes for xbox 360 games  |  28 March, 2014 at 04:51

    A mob of fun-loving zombies is about to invade your home, and your only defense is an arsenal of 49 zombie-zapping plants.

    Moreover, the re-release will also be available as a digital download on
    the Playstation Store and Xbox Live Marketplace.
    The 1st winning strategy is to play European roulette, or as specialist roulette players phone it, simple roulette.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


IT Passion’s Store

Archives

Communities

Get the Source
OSGi supporter
JUG Milano

Upcoming Events



....

Blog Stats

  • 328,400 hits

My PageRank

What's My Google PageRank?

%d bloggers like this: