The document discusses XQuery, a standardized language for querying XML documents. It introduces some key concepts about XQuery, including that it is to XML as SQL is to relational databases, and that XQuery queries are built using XPath expressions. It then provides several examples of XQuery expressions to query an XML document containing book data.
2. 20/1/2009 2
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.
4. doc("books.xml")
<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>
The doc() function is used to open an XML document
doc("books.xml")/bookstore/book/title
doc() function
14. <?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>
<book>1. Everyday Italian</book>
<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>
<book>4. Learning XML</book>
for $x at $i
in /bookstore/book/title
return <book>{$i}. {data($x)}</book>
The at keyword can be used to
count the iteration
at
15. <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 in (10,20), $y in (100,200)
return <test>x={$x} and 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>
for $x at $i in (10,20), $y at $j in (100,200)
return <test>{$i}. x={$x} and {$j}. y={$y}</test>
16. <?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>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
for $b in //book
where some $p in $b/author satisfies
(contains($p,"in"))
return $b/title
<title lang="en">Harry Potter</title>
for $b in //book
where every $p in $b/author satisfies
(contains($p,"in"))
return $b/title
some, every, in, satisfies
17. <?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>
<s>149.93</s>
let $p := for $b in //book
return number($b/price)
return <s>{sum($p)}</s>
<s>30 29.99 49.99 39.95</s>
let $p := for $b in //book
return number($b/price)
return <s>{$p}</s>
sum
22. 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
23. 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)
substring(string,start)
Returns the substring from the start position to the specified
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)
string-length()
Returns the length of the specified string. If there is no string
argument it returns the length of the string value of the current
node
normalize-space(string)
normalize-space()
Removes leading and trailing spaces from the specified string,
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
24. 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
25. 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