XQuery
XQuery
overview
What is XQuery?
- XQuery is to XML what SQL is to databases.
- doc("books.xml")/bookstore/book[price<30]
What is FLWOR?
- FLWOR (pronounced "flower") is an acronym for "For, Let, Where, Order by,
Return".
FLWOR:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
Present the Result In an HTML List
FLWOR:
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x
=>
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>
<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>
Present the Result In an HTML List
cont..
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>
=>
<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 different kinds of nodes:
- element
- attribute
- text
- namespace
- processing-instruction,
- comment
- XQuery is case-sensitive
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>
Note: parentheses around the if expression are required. else is required, but it can be just else ().
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
The for Clause
The for clause binds a variable to each item returned by the in expression. The for clause results in
iteration. There can be multiple for clauses in the same FLWOR expression.
To loop a specific number of times in a for clause, you may use the to keyword:
- for $x in (1 to 5)
return <test>{$x}</test>
Result:
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
The for Clause cont..
The at keyword can be used to count the iteration:
- for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>
Result:
<book>1. Everyday Italian</book>
<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>
<book>4. Learning XML</book>
- It is also allowed with more than one in expression in the for clause. Use comma to separate each
in expression:
- for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>
Result:
<test>x=10 and y=100</test>
<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>
The let Clause
The let clause allows variable assignments and it avoids repeating the same expression many times.
The let clause does not result in iteration.
let $x := (1 to 5)
return <test>{$x}</test>
Result:
<test>1 2 3 4 5</test>
The where Clause
The where clause is used to specify one or more criteria for the result:
for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title
Result:
<title lang="en">Harry Potter</title>
<title lang="en">Everyday Italian</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title
The return Clause
The return clause specifies what is to be returned.
for $x in doc("books.xml")/bookstore/book
return $x/title
Result:
<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>
Examples of Function Calls
Look at the examples below:
Example 1: In an element
<name>{upper-case($booktitle)}</name>