WHAT I CAN STUDY TODAY
Hand Picking From Database For Specially You... :)
 

XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.

XQuery Functions

XQuery is built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.
You can also define your own functions in XQuery.

XQuery Data Types

XQuery shares the same data types as XML Schema 1.0 (XSD).

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>


 

The XML Example Document

We will use the "books.xml" document in the examples below (same XML file as in the previous chapters).

Selecting and Filtering Elements

As we have seen in the previous chapters, we are selecting and filtering elements with either a Path expression or with a FLWOR expression.
Look at the following FLWOR expression:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
  • for - (optional) binds a variable to each item returned by the in expression
  • let - (optional)
  • where - (optional) specifies a criteria
  • order by - (optional) specifies the sort-order of the result
  • return - specifies what to return in the result

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>
It is also allowed with more than one in expression in the for clause. Use comma to separate each in expression:
for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>
Result:
<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>

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>


 

The XML Example Document

We will use the "books.xml" document in the examples below (same XML file as in the previous chapters).

Adding Elements and Attributes to the Result

As we have seen in a previous chapter, we may include elements and attributes from the input document ("books.xml) in the result:
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x
The XQuery expression above will include both the title element and the lang attribute in the result, like this:
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
The XQuery expression above returns the title elements the exact same way as they are described in the input document.
We now want to add our own elements and attributes to the result!

Add HTML Elements and Text

Now, we want to add some HTML elements to the result. We will put the result in an HTML list - together with some text:
<html>
<body>

<h1>Bookstore</h1>

<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
}
</ul>

</body>
</html>
The XQuery expression above will generate the following result:
<html>
<body>

<h1>Bookstore</h1>

<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>

</body>
</html>

Add Attributes to HTML Elements

Next, we want to use the category attribute as a class attribute in the HTML list:
<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>
The XQuery expression above will generate the following result:
<html>
<body>
<h1>Bookstore</h1>

<ul>
<li class="COOKING">Everyday Italian</li>
<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul>

</body>
</html>


 

XQuery is case-sensitive and XQuery elements, attributes, and variables must be valid XML names.

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 :)

The XML Example Document

We will use the "books.xml" document in the examples below (same XML file as in the previous chapters).

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>

XQuery Comparisons

In XQuery there are two ways of comparing values.
1. General comparisons: =, !=, <, <=, >, >=
2. Value comparisons: eq, ne, lt, le, gt, ge
The difference between the two comparison methods are shown below.
The following expression returns true if any q attributes have a value greater than 10:
$bookstore//book/@q > 10
The following expression 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:
$bookstore//book/@q gt 10


 

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.

XQuery Terminology

Nodes

In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).
Look at the following XML document:
<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book>
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>
Example of nodes in the XML document above:
<bookstore> (document node)

<author>J K. Rowling</author> (element node)

lang="en" (attribute node)

Atomic values

Atomic values are nodes with no children or parent.
Example of atomic values:
J K. Rowling

"en"

Items

Items are atomic values or nodes.

Relationship of Nodes

Parent

Each element and attribute has one parent.
In the following example; the book element is the parent of the title, author, year, and price:
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Children

Element nodes may have zero, one or more children.
In the following example; the title, author, year, and price elements are all children of the book element:
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Siblings

Nodes that have the same parent.
In the following example; the title, author, year, and price elements are all siblings:
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Ancestors

A node's parent, parent's parent, etc.
In the following example; the ancestors of the title element are the book element and the bookstore element:
<bookstore>

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

Descendants

A node's children, children's children, etc.
In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:
<bookstore>

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>