Skip to content

XQuery Select and Filter

How to select and filter XML elements in XQuery using path expressions and FLWOR expressions with for, where, order by, and return clauses.

In XQuery you can select and filter elements with either a Path expression or with a FLWOR expression.

Let's see with an example.

Selecting and Filtering Elements

Consider the following example that contains information about a bookstore.

books.xmlxml
<?xml version="1.0" encoding="UTF-8"?>

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Tips</title>
		<author>Tom Nolan</author>
		<year>2022</year>
		<price>39.99</price>
	</book>
	<book category="web">
		<title lang="en">Learn XML</title>
		<author>Tutorial Reference</author>
		<year>2022</year>
		<price>9.99</price>
	</book>
</bookstore>

Using FLWOR Expression

Consider the following FLWOR expression:

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

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.

For example, to loop a specific number of times in a for clause you can use the to keyword:

for $x in (1 to 5)  
return <example>{$x}</example>

and the result is:

<example>1</example>  
<example>2</example>  
<example>3</example>  
<example>4</example>  
<example>5</example>

For example, you can use the at keyword to count the iteration:

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

and the result is:

<book>1. Harry Potter</book>
<book>2. XQuery Tips</book>
<book>3. Learn XML</book>

It is also allowed with more than one expression in the for clause. Use the comma to separate each expression in:

for $x in (10,20), $y in (100,200)  
return <example>x={$x} and y={$y}</example>

and the result is:

<example>x=10 and y=100</example>
<example>x=10 and y=200</example>
<example>x=20 and y=100</example>
<example>x=20 and y=200</example>

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 <example>{$x}</example>

and the result:

<example>1 2 3 4 5</example>

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.

For example, you can order the result by category and title:

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

and the result is sorted by the title element:

<title lang="en">Harry Potter</title>
<title lang="en">Learn XML</title>
<title lang="en">XQuery Tips</title>

The return Clause

The return clause specifies what is to be returned.

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

and the result is:

<title lang="en">Harry Potter</title>
<title lang="en">XQuery Tips</title>
<title lang="en">Learn XML</title>
Navigation