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.
FLWOR expression
FLWOR is an acronym for "For, Let, Where, Order by, Return".
Example WHERE Clause
The where clause is used to specify one or more criteria for the result
where $x/price>30 and $x/price<100
Example 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>
Example 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>
Example FLWOR
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
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.
FLWOR expression
FLWOR is an acronym for "For, Let, Where, Order by, Return".
- 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.
Example 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>
Example 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>
Example WHERE Clause
The where clause is used to specify one or more criteria for the result
where $x/price>30 and $x/price<100
Example 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>
Example 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>
Example FLWOR
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