Skip to main content

XQuery FLOWR

What is FLWOR?

FLWOR is an acronym that stands for "For, Let, Where, Order by, Return".

The following list shows what they account for in a FLWOR expression:

  • For: selects a sequence of nodes
  • Let: binds a sequence to a variable
  • Where: filters the nodes
  • Order by: sorts the nodes
  • Return: what to return (gets evaluated once for every node)

Example

Consider the following example that contains information about a bookstore.

We will use a FLOWR expression to retrieve the authors of these books with a price lower than 20.

books.xml
<?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>

And the following XQuery document contains the query expression to be executed on the above XML document:

books.xqy
let $books := (doc("books.xml")/bookstore/book)
return <results>
{
for $x in $books
where $x/price<20
order by $x/author
return $x/author
}
</results>

More in details:

  • 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 lower than 20.
  • The order by clause defines the sort-order. Will be sort by the author element.
  • The return clause specifies what should be returned. Here it returns the title elements.

The result of the XQuery expression above is:

<author>Tutorial Reference</author>

Table of Contents