Home arrow XML arrow Page 3 - XQuery
XML

XQuery


XQuery speeds up the process of finding information contained in an XML document, which is very handy when dealing with long XML documents. This article, the first of two parts, will teach you how to write XQuery expressions. It is excerpted from chapter nine of the book XML DeMYSTiFieD, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
February 09, 2006
TABLE OF CONTENTS:
  1. · XQuery
  2. · Testing Saxon-B
  3. · How XQuery Works
  4. · A Walkthrough of an XQuery
  5. · Constructors

print this article
SEARCH DEVARTICLES

XQuery - How XQuery Works
(Page 3 of 5 )

An XQuery must contain a conditional expression that specifies the search criteria. A conditional expression is either true or false. For example, /catalog/cd/title = ‘XML Demystified’ is a conditional expression. Saxon-B looks at the next title tag in the XML document and determines if the text of the title tag is ‘XML Demystified’. If so, then the conditional expression is true; otherwise, the conditional expression is false.

A conditional expression is used within a FLWOR expression. FLWOR sounds like more gibberish, but it’s really an acronym for for, let, where, order by, and return clauses. A clause is a component of an XQuery.

For, Let, and Order By Clauses

You use the for and let clauses to assign values to variables within the XQuery. A variable is a placeholder for a value such as $x. Look at the catalog.xq file and you’ll notice the following for clause. For each title tag in the catalog XML file, Saxon-B assigns the text of the current title XML tag to the $x variable and then sorts the titles.

for $x in doc("catalog.xml")/catalog/cd/title

Look at the code that follows the for clause and you’ll see that variable $x is used by the order by clause. The text of the title replaces the $x variable when Saxon-B runs the XQuery before processing the order by clause.

The order by clause places the value of $x in sorted order. In this example, the order by clause sorts all the titles in alphabetical order before storing the titles into the output.html file. Titles within the XML document remain unchanged.

You can specify the direction of the sort by using ascending or descending, as we show here. The default direction is ascending.

order by $x descending

The let clause, not used in the catalog.xq example, assigns a value to a variable. Suppose you want to assign the title ‘XML Demystified’ to variable $x. Here’s how you write the let clause to do this.You can then use $x in place of ‘XML Demystified’ throughout the XQuery.

let $x := 'XML Demystified'

The Where and Return Clauses

You use the where clause to specify a filter criterion using a conditional expression. Let’s say that you want to see titles by Jimi Hendrix. You can use the where clause to tell Saxon-B to compare the text of the artist tag to Jimi Hendrix. If there is a match, then the text of the title tag is assigned the variable $x, which is then used by the order by clause to sort all titles by Jimi Hendrix. This is illustrated in the following segment of the XQuery.

for $x in doc("catalog.xml")/catalog/cd/title
  
where doc("catalog.xml")/catalog/cd/artist = 'Jimi Hendrix'
order by $x

The return clause identifies information that Saxon-B returns to the output.html file. It can return literal characters, the content of tags in an XML document, and the value of variables used within the XQuery.

The catalog.xq XQuery uses the return clause to return HTML characters and the value of the $x. The HTML characters format the data for the browser. The data($x) function is called to return the text associated with $x. Remember $x is the title element that includes the markup tag and any child nodes. The data() function extracts the text from between the tags, for example:

$x equals: <title>How to Dismantle an Atomic Bomb</title>
data($x) equals: How to Dismantle an Atomic Bomb

A function is a task that Saxon-B performs. You call the function by name anytime you want the task to be performed in your XQuery. This is referred to as a function call and must be contained with French braces ({ }). This example calls the data() function, which extracts the text from the argument:

return <li>{data($x)}</li>


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials