XQuery
Raji GHAWI
20/1/2009
What is XQuery ?
XQuery is a standardized language that can be used to query
XML documents.
XQuery is to XML as SQL is to relational databases.
XQuery queries of XML data are built using XPath
expressions.
20/1/2009 2
<?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>
doc() function
doc("books.xml")
The doc() function is used to open an XML document
doc("books.xml")/bookstore/book/title
<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>
XPath expressions <?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
/bookstore/book/title <author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Everyday Italian</title> <title lang="en">Harry Potter</title>
<title lang="en">Harry Potter</title> <author>J K. Rowling</author>
<title lang="en">XQuery Kick Start</title> <year>2005</year>
<price>29.99</price>
<title lang="en">Learning XML</title>
</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>
Simple query with predicate <?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>
/bookstore/book[price<30]
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<book category="CHILDREN"> <author>J K. Rowling</author>
<title lang="en">Harry Potter</title> <year>2005</year>
<author>J K. Rowling</author> <price>29.99</price>
<year>2005</year> </book>
<price>29.99</price> <book category="WEB">
<title lang="en">XQuery Kick Start</title>
</book> <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>
FLWOR
FLWOR stands for:
FOR
LET
WHERE
ORDER BY
RETURN
FLWOR example <?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>
/bookstore/book[price>30]/title </book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
for $x in /bookstore/book <year>2005</year>
where $x/price>30 <price>29.99</price>
</book>
return $x/title <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>
<title lang="en">XQuery Kick Start</title>
<year>2003</year>
<title lang="en">Learning XML</title> <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>
order by <?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>
for $x in /bookstore/book
<price>30.00</price>
where $x/price>30 </book>
Order by $x/title <book category="CHILDREN">
<title lang="en">Harry Potter</title>
return $x/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>
<title lang="en">Learning XML</title>
<year>2003</year>
<title lang="en">XQuery Kick Start</title> <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>
XML output <?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<ul> <year>2005</year>
<price>30.00</price>
{ </book>
for $x in /bookstore/book/title <book category="CHILDREN">
order by $x <title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
return <li>{$x}</li> <year>2005</year>
} <price>29.99</price>
</book>
</ul> <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>
<ul>
<author>Vaidyanathan Nagarajan</author>
<li><title lang="en">Everyday Italian</title></li>
<year>2003</year>
<li><title lang="en">Harry Potter</title></li>
<price>49.99</price>
<li><title lang="en">Learning XML</title></li>
</book>
<li><title lang="en">XQuery Kick Start</title></li>
<book category="WEB">
</ul>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
data() function <?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>
<ul> <price>30.00</price>
</book>
{
<book category="CHILDREN">
for $x in /bookstore/book/title <title lang="en">Harry Potter</title>
order by $x <author>J K. Rowling</author>
<year>2005</year>
return <li>{data($x)}</li> <price>29.99</price>
} </book>
<book category="WEB">
</ul>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<ul> <author>James Linn</author>
<li>Everyday Italian</li> <author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<li>Harry Potter</li>
<price>49.99</price>
<li>Learning XML</li> </book>
<li>XQuery Kick Start</li> <book category="WEB">
</ul> <title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
if-then-else <?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>
for $x in /bookstore/book <price>30.00</price>
</book>
return if ($x/@category="CHILDREN")
<book category="CHILDREN">
then <child>{data($x/title)}</child> <title lang="en">Harry Potter</title>
else <adult>{data($x/title)}</adult> <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>
<adult>Everyday Italian</adult> <author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<child>Harry Potter</child>
<price>49.99</price>
<adult>Learning XML</adult> </book>
<adult>XQuery Kick Start</adult> <book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
FOR and LET
FOR LET
generates a list of bindings of the generates a single binding of the
variable to each element variable to the list of elements
for $x in (1 to 5) let $x := (1 to 5)
return <test>{$x}</test> return <test>{$x}</test>
<test>1</test>
<test>2</test>
<test>3</test> <test>1 2 3 4 5</test>
<test>4</test>
<test>5</test>
at <?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>
for $x at $i <price>30.00</price>
in /bookstore/book/title </book>
<book category="CHILDREN">
return <book>{$i}. {data($x)}</book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
The at keyword can be used to </book>
<book category="WEB">
count the iteration <title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<book>1. Everyday Italian</book> <author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<book>2. Harry Potter</book>
<price>49.99</price>
<book>3. XQuery Kick Start</book> </book>
<book>4. Learning XML</book> <book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>
<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>
for $x at $i in (10,20), $y at $j in (100,200)
return <test>{$i}. x={$x} and {$j}. y={$y}</test>
<test>1. x=10 and 1. y=100</test>
<test>1. x=10 and 2. y=200</test>
<test>2. x=20 and 1. y=100</test>
<test>2. x=20 and 2. y=200</test>
some, every, in, satisfies <?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
for $b in //book <author>Giada De Laurentiis</author>
where some $p in $b/author satisfies <year>2005</year>
<price>30.00</price>
(contains($p,"in")) </book>
return $b/title <book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<title lang="en">Harry Potter</title> <year>2005</year>
<price>29.99</price>
<title lang="en">XQuery Kick Start</title>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
for $b in //book <author>Kurt Cagle</author>
<author>James Linn</author>
where every $p in $b/author satisfies <author>Vaidyanathan Nagarajan</author>
(contains($p,"in")) <year>2003</year>
return $b/title <price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<title lang="en">Harry Potter</title> <author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
sum <?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
let $p := for $b in //book <author>Giada De Laurentiis</author>
return number($b/price) <year>2005</year>
<price>30.00</price>
return <s>{$p}</s>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<s>30 29.99 49.99 39.95</s> <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>
let $p := for $b in //book <author>Kurt Cagle</author>
<author>James Linn</author>
return number($b/price)
<author>Vaidyanathan Nagarajan</author>
return <s>{sum($p)}</s> <year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<s>149.93</s>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Sequence <?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>
let $p := //book/price </book>
<book category="CHILDREN">
let $y := //book/year
<title lang="en">Harry Potter</title>
return ($p, $y) <author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<price>30.00</price> <author>James McGovern</author>
<price>29.99</price> <author>Per Bothner</author>
<author>Kurt Cagle</author>
<price>49.99</price>
<author>James Linn</author>
<price>39.95</price> <author>Vaidyanathan Nagarajan</author>
<year>2005</year> <year>2003</year>
<year>2005</year> <price>49.99</price>
<year>2003</year> </book>
<book category="WEB">
<year>2003</year>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
union <?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>
//book/(price union year) <price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
//book/(price | title)
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<year>2005</year> <title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<price>30.00</price>
<author>Per Bothner</author>
<year>2005</year> <author>Kurt Cagle</author>
<price>29.99</price> <author>James Linn</author>
<year>2003</year> <author>Vaidyanathan Nagarajan</author>
<price>49.99</price> <year>2003</year>
<price>49.99</price>
<year>2003</year>
</book>
<price>39.95</price> <book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
intersect <?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>
let $b := //book <price>30.00</price>
return $b[year = "2005"] intersect </book>
$b[number(price) >= 30] <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>
<book category="COOKING">
<author>Per Bothner</author>
<title lang="en">Everyday Italian</title> <author>Kurt Cagle</author>
<author>Giada De Laurentiis</author> <author>James Linn</author>
<year>2005</year> <author>Vaidyanathan Nagarajan</author>
<price>30.00</price> <year>2003</year>
<price>49.99</price>
</book>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
except <?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
//book/(* except author)
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<title lang="en">Everyday Italian</title> <author>J K. Rowling</author>
<year>2005</year>
<year>2005</year>
<price>29.99</price>
<price>30.00</price> </book>
<title lang="en">Harry Potter</title> <book category="WEB">
<year>2005</year> <title lang="en">XQuery Kick Start</title>
<price>29.99</price> <author>James McGovern</author>
<author>Per Bothner</author>
<title lang="en">XQuery Kick Start</title>
<author>Kurt Cagle</author>
<year>2003</year> <author>James Linn</author>
<price>49.99</price> <author>Vaidyanathan Nagarajan</author>
<title lang="en">Learning XML</title> <year>2003</year>
<year>2003</year> <price>49.99</price>
</book>
<price>39.95</price>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Functions on Numeric Values
number(arg) Returns the numeric value of the argument
abs(num) Returns the absolute value of the argument
ceiling(num) Returns the smallest integer that is greater than the number argument
floor(num) Returns the largest integer that is not greater than the number argument
round(num) Rounds the number argument to the nearest integer
Functions on Strings - 1
string(arg) Returns the string value of the argument
compare(comp1,comp2) Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to
comp2, or 1 if comp1 is greater than comp2
concat(string,string,...) Returns the concatenation of the strings
string-join((string,string,...),sep) Returns a string created by concatenating the string arguments
and using the sep argument as the separator
substring(string,start,len) Returns the substring from the start position to the specified
substring(string,start) length. Index of the first character is 1. If length is omitted it
returns the substring from the start position to the end
string-length(string) Returns the length of the specified string. If there is no string
string-length() argument it returns the length of the string value of the current
node
normalize-space(string) Removes leading and trailing spaces from the specified string,
normalize-space() and replaces all internal sequences of white space with one
and returns the result. If there is no string argument it does the
same on the current node
Functions on Strings - 2
upper-case(string) Converts the string argument to upper-case
lower-case(string) Converts the string argument to lower-case
contains(string1,string2) Returns true if string1 contains string2, otherwise it returns
false
starts-with(string1,string2) Returns true if string1 starts with string2, otherwise it returns
false
ends-with(string1,string2) Returns true if string1 ends with string2, otherwise it returns
false
substring-before(string1,string2) Returns the start of string1 before string2 occurs in it
substring-after(string1,string2) Returns the remainder of string1 after string2 occurs in it
matches(string,pattern) Returns true if the string argument matches the pattern,
otherwise, it returns false
replace(string,pattern,replace) Returns a string that is created by replacing the given pattern
with the replace argument
Aggregate Functions
count((item,item,...)) Returns the count of nodes
avg((arg,arg,...)) Returns the average of the argument values
max((arg,arg,...)) Returns the argument that is greater than the others
min((arg,arg,...)) Returns the argument that is less than the others
sum(arg,arg,...) Returns the sum of the numeric value of each node in the specified
node-set
References
http://www.w3schools.com/xquery
http://www.w3schools.com/xpath/xpath_functions.asp
http://www.brics.dk/~amoeller/XML/querying/
XQuery
Priscilla Walmsley
O'Reilly Media, Inc. (2007)
(ISBN 0596006349)