XQuery Tutorial on 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.

example

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.

books.xml

<?xml version="1.0" encoding="utf-8"?>
<books>
   
   <book category="java">
      <title lang="en">learn java in 24 hours</title>
      <author>robert</author>
      <year>2005</year>
      <price>30.00</price>
   </book>
   
   <book category="dotnet">
      <title lang="en">learn .net in 24 hours</title>
      <author>peter</author>
      <year>2011</year>
      <price>70.50</price>
   </book>
   
   <book category="xml">
      <title lang="en">learn xquery in 24 hours</title>
      <author>robert</author>
      <author>peter</author> 
      <year>2013</year>
      <price>50.00</price>
   </book>
   
   <book category="xml">
      <title lang="en">learn xpath in 24 hours</title>
      <author>jay ban</author>
      <year>2010</year>
      <price>16.50</price>
   </book>
   
</books>

the following xquery document contains the query expression to be executed on the above xml document.

books.xqy

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

result

<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.