XQuery is About Querying XML
XQuery is a language for finding and extracting elements and attributes from XML documents.
Here is an example of what XQuery could solve:
“Select all CD records with a price less than $10 from the CD collection stored in cd_catalog.xml”
XQuery for XML is like SQL for databases.
XQuery and XPath
XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. If you have already studied XPath you will have no problems with understanding XQuery.
XQuery FLWOR Expressions
How to Select Nodes From “books.xml” With FLWOR
Look at the following path expression:
doc("books.xml")/bookstore/book[price>30]/title
The expression above will select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30.
The following FLWOR expression will select exactly the same as the path expression above:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
The result will be:
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
With FLWOR you can sort the result:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
The for clause selects all book elements under the bookstore element into a variable called $x.
The where clause selects only book elements with a price element with a value greater than 30.
The order by clause defines the sort-order. Will be sort by the title element.
The return clause specifies what should be returned. Here it returns the title elements.
The result of the XQuery expression above will be:
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
Present the Result In an HTML List
Look at the following XQuery FLWOR expression:
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x
The expression above will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order.
Now we want to list all the book-titles in our bookstore in an HTML list. We add <ul>
and <li>
tags to the FLWOR expression:
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>
The result of the above will be:
<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>
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 Conditional Expressions
“If-Then-Else” expressions are allowed in XQuery.
Look at the following example:
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>
Notes on the “if-then-else” syntax: parentheses around the if expression are required. else is required, but it can be just else ().
The result of the example above will be:
<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>XQuery Kick Start</adult>
<adult>Learning XML</adult>
The for Clause
The for clause binds a variable to each item returned by the in expression. The for clause results in iteration. There can be multiple for clauses in the same FLWOR expression.
To loop a specific number of times in a for clause, you may use the to keyword:
for $x in (1 to 5)
return <test>{$x}</test>
Result:
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
The at keyword can be used to count the iteration:
for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>
Result:
<book>1. Everyday Italian</book>
<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>
<book>4. Learning XML</book>
The let Clause
The let clause allows variable assignments and it avoids repeating the same expression many times. The let clause does not result in iteration.
let $x := (1 to 5)
return <test>{$x}</test>
Result:
<test>1 2 3 4 5</test>
The where Clause
The where clause is used to specify one or more criteria for the result:
where $x/price>30 and $x/price<100
The order by Clause
The order by clause is used to specify the sort order of the result. Here we want to order the result by category and title:
for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title
Result:
<title lang="en">Harry Potter</title>
<title lang="en">Everyday Italian</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
The return Clause
The return clause specifies what is to be returned.
for $x in doc("books.xml")/bookstore/book
return $x/title
Result:
<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>
Examples of Function Calls
A call to a function can appear where an expression may appear. Look at the examples below:
Example 1: In an element
<name>{upper-case($booktitle)}</name>
Example 2: In the predicate of a path expression
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']
Example 3: In a let clause
let $name := (substring($booktitle,1,4))
XQuery User-Defined Functions
If you cannot find the XQuery function you need, you can write your own.
User-defined functions can be defined in the query or in a separate library.
Syntax
declare function _prefix:function_name_($_parameter as datatype_)
as _returnDatatype_
{
..._function code here_...
};
Notes on user-defined functions:
- Use the declare function keyword
- The name of the function must be prefixed
- The data type of the parameters are mostly the same as the data types defined in XML Schema
- The body of the function must be surrounded by curly braces
Example of a User-defined Function Declared in the Query
declare function local:minPrice($p as xs:decimal?,$d as xs:decimal?)
as xs:decimal?
{
let $disc := ($p * $d) div 100
return ($p - $disc)
};
Below is an example of how to call the function above:
<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>
Reference
https://www.w3schools.com/xml/xquery_intro.asp