Sunday, November 15, 2009








XQuery is to XML what SQL is to database tables.

XQuery was designed to query XML data.

XQuery Example

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


Introduction to XQuery

XQuery is to XML what SQL is to database tables.

XQuery is designed to query XML data - not just XML files, but anything that can appear as XML, including databases.
What You Should Already Know

Before you continue you should have a basic understanding of the following:

    * HTML / XHTML
    * XML / XML Namespaces
    * XPath

What is XQuery?


    *  XQuery is the language for querying XML data
    * XQuery for XML is like SQL for databases
    * XQuery is built on XPath expressions
    * XQuery is supported by all major databases
    * XQuery is a W3C Recommendation

 XQuery is About Querying XML

XQuery is a language for finding and extracting elements and attributes from XML documents.

Here is an example of a question that XQuery could solve:

"Select all CD records with a price less than $10 from the CD collection stored in the XML document called cd_catalog.xml"
XQuery and XPath

XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. If you have already studied XPath you will have no problems with understanding XQuery.

You can read more about XPath in our XPath Tutorial.
XQuery - Examples of Use

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

XQuery is a W3C Recommendation

XQuery is compatible with several W3C standards, such as XML, Namespaces, XSLT, XPath, and XML Schema.

XQuery 1.0 became a W3C Recommendation January 23, 2007.

The XML Example Document

We will use the following XML document in the examples below.

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

View the "books.xml" file in your browser.
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>
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML

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>

How to Select Nodes From "books.xml" With FLWOR

Look at the following path expression:
doc("books.xml")/bookstore/book[price>30]/title

The expression above will select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30.

The following FLWOR expression will select exactly the same as the path expression above:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title

The result will be:
title lang="en">XQuery Kick Start/title>
title lang="en">Learning XML/title>

With FLWOR you can sort the result:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

FLWOR is an acronym for "For, Let, Where, Order by, Return".

The for clause selects all book elements under the bookstore element  into a variable called $x.

The where clause selects only book elements with a price element with a value greater than 30.

The order by clause defines the sort-order. Will be sort by the title element.

The return clause specifies what should be returned. Here it returns the title elements.

The result of the XQuery expression above will be:
title lang="en">Learning XML/title>
title lang="en">XQuery Kick Start/title>



Present the Result In an HTML List

Look at the following XQuery FLWOR expression:

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

The expression above will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order.

Now we want to list all the book-titles in our bookstore in an HTML list. We add ul> and li> tags to the FLWOR expression:

ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return li>{$x}/li>
}
/ul>

The result of the above will be:

ul>
li>title lang="en">Everyday Italian/title>/li>
li>title lang="en">Harry Potter/title>/li>
li>title lang="en">Learning XML/title>/li>
li>title lang="en">XQuery Kick Start/title>/li>
/ul>

Now we want to eliminate the title element, and show only the data inside the title element:

ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return li>{data($x)}/li>
}
/ul>

The result will be (an HTML list):
ul>
li>Everyday Italian/li>
li>Harry Potter/li>
li>Learning XML/li>
li>XQuery Kick Start/li>
/ul>

XQuery Terms

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.

XQuery Terminology

Nodes

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).

Look at the following XML document:
?xml version="1.0" encoding="ISO-8859-1"?>

bookstore>

book>
  title lang="en">Harry Potter/title>
  author>J K. Rowling/author>
  year>2005/year>
  price>29.99/price>
/book>

/bookstore>

Example of nodes in the XML document above:

bookstore> (document node)

author>J K. Rowling/author> (element node)

lang="en" (attribute node)

Atomic values

Atomic values are nodes with no children or parent.

Example of atomic values:
J K. Rowling

"en"
Items

Items are atomic values or nodes.
Relationship of Nodes
Parent

Each element and attribute has one parent.

In the following example; the book element is the parent of the title, author, year, and price:
book>
  title>Harry Potter/title>
  author>J K. Rowling/author>
  year>2005/year>
  price>29.99/price>
/book>
Children

Element nodes may have zero, one or more children.

In the following example; the title, author, year, and price elements are all children of the book element:
book>
  title>Harry Potter/title>
  author>J K. Rowling/author>
  year>2005/year>
  price>29.99/price>
/book>
Siblings

Nodes that have the same parent.

In the following example; the title, author, year, and price elements are all siblings:
book>
  title>Harry Potter/title>
  author>J K. Rowling/author>
  year>2005/year>
  price>29.99/price>
/book>
Ancestors

A node's parent, parent's parent, etc.

In the following example; the ancestors of the title element are the book element and the bookstore element:
bookstore>

book>
  title>Harry Potter/title>
  author>J K. Rowling/author>
  year>2005/year>
  price>29.99/price>
/book>

/bookstore>
Descendants

A node's children, children's children, etc.

In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:
bookstore>

book>
  title>Harry Potter/title>
  author>J K. Rowling/author>
  year>2005/year>
  price>29.99/price>
/book>

/bookstore>


XQuery Syntax

XQuery is case-sensitive and XQuery elements, attributes, and variables must be valid XML names.
XQuery Basic Syntax Rules

Some basic syntax rules:

    * XQuery is case-sensitive
    * XQuery elements, attributes, and variables must be valid XML names
    * An XQuery string value can be in single or double quotes
    * An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
    * XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)

XQuery Conditional Expressions

"If-Then-Else" expressions are allowed in XQuery.

Look at the following example:

for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
then child>{data($x/title)}/child>
else adult>{data($x/title)}/adult>

Notes on the "if-then-else" syntax: parentheses around the if expression are required. else is required, but it can be just else ().

The result of the example above will be:

adult>Everyday Italian/adult>
child>Harry Potter/child>
adult>Learning XML/adult>
adult>XQuery Kick Start/adult>

XQuery Comparisons

In XQuery there are two ways of comparing values.

1. General comparisons: =, !=, , =, >, >=

2. Value comparisons: eq, ne, lt, le, gt, ge

The difference between the two comparison methods are shown below.

The following expression returns true if any q attributes have a value greater than 10:
$bookstore//book/@q > 10

The following expression returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs:
$bookstore//book/@q gt 10

No comments:

Post a Comment