7/9/2019
Prepared By: Dr.Saranya.K.G 1
XQUERY
Prepared By:Dr.Saranya K.G
What is Xquery?
• XQuey is a standardized language that can be used to query XML
documents.
• XQuery queries of XML data are built using XPath expressions.
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 2
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
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>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 3
Xpath Expressions
Prepared By:Dr.Saranya K.G
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </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>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 4
FLWOR
FLWOR stands for:
• FOR - selects a sequence of nodes
• LET - binds a sequence to a variable
• WHERE - filters the nodes
• ORDER BY - sorts the nodes
• RETURN - what to return (gets evaluated once
for every node)
Prepared By:Dr.Saranya K.G
FLWOR example
/bookstore/book[price>30]/title
for $x in /bookstore/book
where $x/price>30
return $x/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 Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 5
ORDER BY
for $x in /bookstore/book
where $x/price>30
Order by $x/title
return $x/title
<title lang="en"> Learning XML </title>
<title lang="en"> XQuery Kick Start </title>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
XML output
<u1>
{
for $x in /bookstore/book/title
order by $x
return <li>{$x}</li>
}
</u1>
<u1>
<li> <title lang="en"> Everyday Italian</title></li>
<li><title lang="en">Harry Potter</title></li>
<li><title lang="en">Learning XML Start</title></li>
<li><title lang="en">XQuery Kick Start</title></li>
</u1>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 6
data() function
<u1>
[
for $x in /bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</u1>
<u1>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</u1>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
if-then-else
for $x in /bookstore/book
return if ($x/@category="CHILDREN")
then <child>{data ($x/title)}</child>
else <adult>{data($x/title)}</adult>
<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 7
FOR and LET
FOR
generates a list of bindings of the
variable to each element.
for $x in (1 to 5)
return <test>{$x}</test>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
LET
generates a single binding of the variable
to the list of elements.
let $x := (1 to 5)
return <test>{$x}</test>
<test> 1 2 2 4 5 </test>
Prepared By:Dr.Saranya K.G
at
for $x at $i
in /bookstore/book/title
return <book>{$i}.{data($x)}</book>
The at keyword can be used to count the iteration.
<book>1. Everyday Italian </book>
<book>2. Harry Potter </book>
<book>3. XQuery Kick Start </book>
<book>4. Learning XML </book>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 8
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>
Prepared By:Dr.Saranya K.G
some, every,in,satisfies
for $b in //book
where some $p in $b/author satisfies
(contains ($p,"in"))
return $b/title
<title lang="en"> Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
for $b in //book
where every $p in $b/author satisfies
(contains($p,"in"))
return $b/title
<title lang="en">Harry Potter</title>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINN </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 9
sum
let $p := for $b in //book return number($b/price)
return <s>{$p}</s>
<s>30 29.99 49.99 39.95 </s>
let $p := for $b in //book return number ($b/price)
return <s> {sum($p)}</s>
<s> 149.93 </s>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
Sequence
let $p := //book/price
let $y := //book/year
return ($p,$y)
<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>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 10
union
//book/(price union year)
//book/(price | title)
<year>2005</year>
<price>30.00</price>
<year>2005</yera>
<price>29.99</price>
<year>2003</year>
<price>49.99</price>
<year>2003</year>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
intersect
let $b := //book
return $b[year="2005"] intersect
$b[number(price)>=30]
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurentiis</author>
<yer>2005</year>
<price>30.00</price>
</book>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 11
except
//book/(* except author)
<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>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en"> Everyday Italian </title>
<author> Giada De Laurenties </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 MCGOVEN </author>
<author> PER BOTHNER </author>
<author> KURT CAGLE </author>
<author> JAMES LINE </author>
<author> GIRIJA VAIDYANATHAN </author>
<year> 2003 </year>
<price> 49.99 </price>
</book>
<book category="WEB">
<title lang="en"> LEARNING XML </title>
<author> ERICK T ROY </author>
<year> 2003 </year>
<price> 39.95 </price>
</book>
</bookstore>
Prepared By:Dr.Saranya K.G
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 Numeric Values
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 12
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) Retruns 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.
Functions on Strings1
Prepared By:Dr.Saranya K.G
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 string 1 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.
FUNCTIONS ON STRINGS2
Prepared By:Dr.Saranya K.G
7/9/2019
Prepared By: Dr.Saranya.K.G 13
count((item,item,…)) Returns the count of the 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.
Aggregate Functions
Prepared By:Dr.Saranya K.G