What is XQuery?
- XQuery is the language for querying XML data
- XQuery for XML is like SQL for databases
- XQuery is built on XPath expressions
- XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.)
Functions
doc("books.xml")
doc("books.xml")/bookstore/book/title
doc("books.xml")/bookstore/book[price < 30 ]
doc("books.xml")/bookstore/book[price > 30]/title
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
< ul >
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return < li > {$x} </ li >
}
</ ul >
< ul >
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return < li > {data($x)} </ li >
}
</ ul >
doc("books.xml")/bookstore/book/title
doc("books.xml")/bookstore/book[price < 30 ]
doc("books.xml")/bookstore/book[price > 30]/title
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
< ul >
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return < li > {$x} </ li >
}
</ ul >
< ul >
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return < li > {data($x)} </ li >
}
</ ul >
XQuery Basic Syntax Rules
Some basic syntax rules:- XQuery is case-sensitive
- XQuery elements, attributes, and variables must be valid XML names
- An XQuery string value can be in single or double quotes
- An XQuery variable is defined with a $ followed by a name, e.g. $bookstore
- XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)
XQuery Basic Syntax Rules
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 >
return if ($x/@category="CHILDREN")
then < child > {data($x/title)} </ child >
else < adult > {data($x/title)} </ adult >
In XQuery there are two ways of comparing values.
1. General comparisons: =, !=, <, <=, >, >=2. Value comparisons: eq, ne, lt, le, gt, ge
$bookstore//book/@q > 10
The expression above returns true if any q attributes have values greater than 10.
$bookstore//book/@q gt 10
The expression above 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.
(: Adding Elements and Attributes to the Result :)
< html >
< body >
< h1 > Bookstore </ h1 >
< ul >
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return < li class ="{data($x/@category)}" > {data($x/title)} </ li >
}
</ ul >
</ body >
</ html >
(: XQuery Selecting and Filtering :)
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
for $x in (1 to 5)
return < test > {$x} </ test >
for $x at $i in doc("books.xml")/bookstore/book/title
return < book > {$i}. {data($x)} </ book >
for $x in (10,20), $y in (100,200)
return < test > x={$x} and y={$y} </ test >
let $x := (1 to 5)
return < test > {$x} </ test >
(:XQuery Built-in Functions :)
< name > {uppercase($booktitle)} </ name >
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']
(: XQuery User-Defined Functions
Syntax :)
declare function prefix:function_name($parameter AS datatype)
AS returnDatatype
{
(: function code here :)
};
declare function local:minPrice(
$price as xs:decimal?,
$discount as xs:decimal?)
AS xs:decimal?
{
let $disc := ($price * $discount) div 100
return ($price - $disc)
};
(: Below is an example of how to call the function above :)
< minPrice > {local:minPrice($book/price, $book/discount)} </ minPrice >
let $name := (substring($booktitle,1,4))
< html >
< body >
< h1 > Bookstore </ h1 >
< ul >
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return < li class ="{data($x/@category)}" > {data($x/title)} </ li >
}
</ ul >
</ body >
</ html >
(: XQuery Selecting and Filtering :)
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
for $x in (1 to 5)
return < test > {$x} </ test >
for $x at $i in doc("books.xml")/bookstore/book/title
return < book > {$i}. {data($x)} </ book >
for $x in (10,20), $y in (100,200)
return < test > x={$x} and y={$y} </ test >
let $x := (1 to 5)
return < test > {$x} </ test >
(:XQuery Built-in Functions :)
< name > {uppercase($booktitle)} </ name >
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']
(: XQuery User-Defined Functions
Syntax :)
declare function prefix:function_name($parameter AS datatype)
AS returnDatatype
{
(: function code here :)
};
declare function local:minPrice(
$price as xs:decimal?,
$discount as xs:decimal?)
AS xs:decimal?
{
let $disc := ($price * $discount) div 100
return ($price - $disc)
};
(: Below is an example of how to call the function above :)
< minPrice > {local:minPrice($book/price, $book/discount)} </ minPrice >
let $name := (substring($booktitle,1,4))
* Reference to W3C Schools