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)
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.
<?xml version="1.0" encoding="UTF-8"?>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<title lang="en">XQuery Tips</title>
<title lang="en">Learn XML</title>
And the following XQuery document contains the query expression to be executed on the above XML document:
let $books := (doc("books.xml")/bookstore/book)
for $x in $books
order by $x/author
More in details:
- The for clause selects all book elements under the bookstore element into a variable called
- 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: