XQuery Selecting and Filtering

Books.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Author Name</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
 <year>2005</year>
    <price>300.00</price>
  </book>
 
  <book category="Science">
    <title lang="en">Ecosystem</title>
    <author>Author Name</author>
 <year>2010</year>
    <price>250.00</price>
  </book>
 
  <book category="GK">
    <title lang="en">Current Affairs</title>
    <author>Author Name</author>
 <year>2004</year>
    <price>500.00</price>
  </book>
 
  <book category="Science">
    <title lang="en">Science Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>
 <year>2011</year>
    <price>150.00</price>
  </book>
 
</bookstore>

Selecting and Filtering Elements:

Using either a Path expression or with a FLWOR expression, we can select and filter the elements.

Example:

for $x in doc("books.xml")/bookstore/book
where $x/price>300
order by $x/title
return $x/title

Explanation:

The above FLWOR expression includes:

  • for: It is used to bind a variable to each item returned by the in expression. It is optional.
  • let: It is optional.
  • where: It is used to specify criteria. It is optional.
  • order by: It is used to specify the sort order of the result. It is optional.
  • return: It is used to specify what to return in the result.

The for Clause:

To bind a variable to each item returned by the in expression, the for clause is used. This clause results in an iteration and we may use the to keyword to loop a specific number of times in a for clause. In a single FLWOR expression, multiple for clauses can be used.

Example 1:

for $x in (5 to 9)
return <num>{$x}</num>

Result:

<num>5</num>
<num>6</num>
<num>7</num>
<num>8</num>
<num>9</num>

Example 2:

for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>

Explanation:

In the above example, we are using the at keyword to count the iteration.

Result:

<book>1. ABC</book>
<book>2. XQuery Book</book>
<book>3. Sociology 1</book>
<book>4. Current Affairs</book>
<book>5. Science Book</book>

Example 3:

for $x in (1,2), $y in (10,20)
return <test>x={$x} and y={$y}</test>

Explanation:

In the above example, we are using multiple “in” expression in the for clause. We are separating each “in” in the expression with a comma.

Result:

<test>x=1 and y=10</test>
<test>x=1 and y=20</test>
<test>x=2 and y=10</test>
<test>x=2 and y=20</test>

The let Clause:

To allow the variable assignments, the let clause is used. It helps in avoiding the repeating of the same expression multiple times. It also does not result in an iteration.

Example:

let $x := (5 to 9)
return <test>{$x}</test>

Result:

<test>5 6 7 8 9</test>

The where Clause:

To specify one or more criteria for the result, the where clause is used.

Example:

where $x/price>200 and $x/price<400

The order by Clause:

To specify the sort order of the result, the order by clause is used.

Example:

for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title

Explanation:

In the above example, we are ordering the result by category and title.

Result:

<title lang="en">ABC</title>
<title lang="en">XQuery Book</title>
<title lang="en">Current Affairs</title>
<title lang="en">Ecosystem</title>
<title lang="en">Science Book</title>

The return Clause:

To specify what is to be returned, the return clause is used.

Example:

for $x in doc("books.xml")/bookstore/book
return $x/title

Result:

<title lang="en">ABC</title>
<title lang="en">XQuery Book</title>
<title lang="en">Ecosystem</title>
<title lang="en">Current Affairs</title>
<title lang="en">Science Book</title>
Please Share