02.06.2013 Views

XML Demystified

XML Demystified

XML Demystified

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

126<br />

How XQuery Works<br />

<strong>XML</strong> Demystifi ed<br />

An XQuery must contain a conditional expression that specifies the search criteria.<br />

A conditional expression is either true or false. For example, /catalog/cd/title =<br />

‘<strong>XML</strong> <strong>Demystified</strong>’ is a conditional expression. Saxon-B looks at the next title tag<br />

in the <strong>XML</strong> document and determines if the text of the title tag is ‘<strong>XML</strong> <strong>Demystified</strong>’.<br />

If so, then the conditional expression is true; otherwise, the conditional expression<br />

is false.<br />

A conditional expression is used within a FLWOR expression. FLWOR sounds<br />

like more gibberish, but it’s really an acronym for for, let, where, order by, and<br />

return clauses. A clause is a component of an XQuery.<br />

For, Let, and Order By Clauses<br />

You use the for and let clauses to assign values to variables within the XQuery. A<br />

variable is a placeholder for a value such as $x. Look at the catalog.xq file and<br />

you’ll notice the following for clause. For each title tag in the catalog <strong>XML</strong> file,<br />

Saxon-B assigns the text of the current title <strong>XML</strong> tag to the $x variable and then<br />

sorts the titles.<br />

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

Look at the code that follows the for clause and you’ll see that variable $x is used<br />

by the order by clause. The text of the title replaces the $x variable when Saxon-B<br />

runs the XQuery before processing the order by clause.<br />

The order by clause places the value of $x in sorted order. In this example, the<br />

order by clause sorts all the titles in alphabetical order before storing the titles into<br />

the output.html file. Titles within the <strong>XML</strong> document remain unchanged.<br />

You can specify the direction of the sort by using ascending or descending, as we<br />

show here. The default direction is ascending.<br />

order by $x descending<br />

The let clause, not used in the catalog.xq example, assigns a value to a variable.<br />

Suppose you want to assign the title ‘<strong>XML</strong> <strong>Demystified</strong>’ to variable $x. Here’s how<br />

you write the let clause to do this. You can then use $x in place of ‘<strong>XML</strong> <strong>Demystified</strong>’<br />

throughout the XQuery.<br />

let $x := '<strong>XML</strong> <strong>Demystified</strong>'<br />

The Where and Return Clauses<br />

You use the where clause to specify a filter criterion using a conditional expression.<br />

Let’s say that you want to see titles by Jimi Hendrix. You can use the where clause

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!