XQuery - 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 −

  • F - For - Selects a collection of all nodes.

  • L - Let - Puts the result in an XQuery variable.

  • W - Where - Selects the nodes specified by the condition.

  • O - Order by - Orders the nodes specified as per criteria.

  • R - Return - Returns the final result.


Following is a sample XML document that contains information on a collection of books. We will use a FLWOR expression to retrieve the titles of those books with a price greater than 30.


<?xml version="1.0" encoding="UTF-8"?>
   <book category="JAVA">
      <title lang="en">Learn Java in 24 Hours</title>
   <book category="DOTNET">
      <title lang="en">Learn .Net in 24 hours</title>
   <book category="XML">
      <title lang="en">Learn XQuery in 24 hours</title>
   <book category="XML">
      <title lang="en">Learn XPath in 24 hours</title>
      <author>Jay Ban</author>

The following Xquery document contains the query expression to be executed on the above XML document.


let $books := (doc("books.xml")/books/book)
return <results>
   for $x in $books
   where $x/price>30
   order by $x/price
   return $x/title


<title lang="en">Learn XQuery in 24 hours</title>
<title lang="en">Learn .Net in 24 hours</title>

Verify Result

To verify the result, replace the contents of books.xqy (given in theEnvironment Setup chapter) with the above XQuery expression and execute the XQueryTester java program.