Upload
harish-bandi
View
453
Download
0
Embed Size (px)
Citation preview
XQuery
Raji GHAWI
20/1/2009
220/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.
<?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("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
XPath expressions <?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>
/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>
<?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>
/bookstore/book[price<30]
<book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book>
Simple query with predicate
FLWOR FLWOR stands for:
FOR LET WHERE ORDER BY RETURN
<?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>
/bookstore/book[price>30]/title
<title lang="en">XQuery Kick Start</title><title lang="en">Learning XML</title>
for $x in /bookstore/bookwhere $x/price>30return $x/title
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> </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">Learning XML</title><title lang="en">XQuery Kick Start</title>
for $x in /bookstore/bookwhere $x/price>30Order by $x/titlereturn $x/title
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> <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>
<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>
<ul>{for $x in /bookstore/book/titleorder by $xreturn <li>{$x}</li>}</ul>
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> <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>
<ul><li>Everyday Italian</li><li>Harry Potter</li><li>Learning XML</li><li>XQuery Kick Start</li></ul>
<ul>{for $x in /bookstore/book/titleorder by $xreturn <li>{data($x)}</li>}</ul>
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> <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>
<adult>Everyday Italian</adult><child>Harry Potter</child><adult>Learning XML</adult><adult>XQuery Kick Start</adult>
for $x in /bookstore/bookreturn if ($x/@category="CHILDREN")then <child>{data($x/title)}</child>else <adult>{data($x/title)}</adult>
if-then-else
<test>1</test><test>2</test><test>3</test><test>4</test><test>5</test>
for $x in (1 to 5)return <test>{$x}</test>
<test>1 2 3 4 5</test>
let $x := (1 to 5)return <test>{$x}</test>
FORgenerates a list of bindings of the variable to each element
LETgenerates a single binding of the variable to the list of elements
FOR and LET
<?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/titlereturn <book>{$i}. {data($x)}</book>
The at keyword can be used to count the iteration
at
<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>
<?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 //bookwhere some $p in $b/author satisfies(contains($p,"in"))return $b/title
<title lang="en">Harry Potter</title>
for $b in //bookwhere every $p in $b/author satisfies(contains($p,"in"))return $b/title
some, every, in, satisfies
<?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
<?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>
<price>30.00</price><price>29.99</price><price>49.99</price><price>39.95</price><year>2005</year><year>2005</year><year>2003</year><year>2003</year>
let $p := //book/pricelet $y := //book/yearreturn ($p, $y)
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> </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>
<year>2005</year><price>30.00</price><year>2005</year><price>29.99</price><year>2003</year><price>49.99</price><year>2003</year><price>39.95</price>
//book/(price union year)
//book/(price | title)
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> <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 category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price></book>
let $b := //bookreturn $b[year = "2005"] intersect
$b[number(price) >= 30]
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> <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">Everyday Italian</title><year>2005</year><price>30.00</price><title lang="en">Harry Potter</title><year>2005</year><price>29.99</price><title lang="en">XQuery Kick Start</title><year>2003</year><price>49.99</price><title lang="en">Learning XML</title><year>2003</year><price>39.95</price>
//book/(* except author)
except
Functions on Numeric Values
number(arg) Returns the numeric value of the argumentabs(num) Returns the absolute value of the argumentceiling(num) Returns the smallest integer that is greater than the number argumentfloor(num) Returns the largest integer that is not greater than the number argumentround(num) Rounds the number argument to the nearest integer
Functions on Strings - 1
string(arg) Returns the string value of the argumentcompare(comp1,comp2) Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to
comp2, or 1 if comp1 is greater than comp2concat(string,string,...) Returns the concatenation of the stringsstring-join((string,string,...),sep) Returns a string created by concatenating the string arguments
and using the sep argument as the separatorsubstring(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
Functions on Strings - 2
upper-case(string) Converts the string argument to upper-caselower-case(string) Converts the string argument to lower-casecontains(string1,string2) Returns true if string1 contains string2, otherwise it returns
falsestarts-with(string1,string2) Returns true if string1 starts with string2, otherwise it returns
falseends-with(string1,string2) Returns true if string1 ends with string2, otherwise it returns
falsesubstring-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 itmatches(string,pattern) Returns true if the string argument matches the pattern,
otherwise, it returns falsereplace(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 nodesavg((arg,arg,...)) Returns the average of the argument valuesmax((arg,arg,...)) Returns the argument that is greater than the othersmin((arg,arg,...)) Returns the argument that is less than the otherssum(arg,arg,...) Returns the sum of the numeric value of each node in the specified node-
set
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)
References