XQuery Tutorial – FLWOR expression

The XQuery Tutorial covers the topics on Xquery Functions, XQuery FLWOR, XQuery Syntax, XQuery Add, XQuery Select , extracting elements and attributes from XML documents, transform XML data to XHTML, XPath Operators , XPath Functions, XQuery Data Types, XQuery User-Defined Functions.

Letd discuss FLWOR expression in detail.

Xquery Tutorial on XQuery FLOWR expression shows how to binding XML nodes, XML sequences to variable, sorting the nodes and expected node return value.

What is FLWOR expression in XQuery ?

FLWOR is an acronym for “For, Let, Where, Order by, Return”. FLWOR is pronounced as “flower”.

For – selects the sequence of nodes
Let – binds the sequence to a variable
Where – use for nodes filtering
Order by – use for nodes sorting
Return – what to return

XQuery FLWOR node retrieval example

Lets consider the below given “order.xml” file and try to get the node retrieval based on price check where price for the order <30.

XQuery FLWOR Expression for price check:

doc(“order.xml”)/oracleappshelpOrder/order[price< 30]/orderId

The XQuery FLWOR Expression returns same result as per the below XQuery XPath expression

for $x in doc(“order.xml”)/oracleappshelpOrder/order
where $x/price<30
return $x/orderId

Xquery Output:

<orderId>12346</orderId>

<orderId>12347</orderId>

<oracleappshelpOrder>
<Order>
    <orderId>12345</orderId>
    <quantity>1</quantity>
  	<price>40<price>
</Order>
<Order>
    <orderId>12346</orderId>
    <quantity>1</quantity>
  	<price>25<price>
</Order>
<Order>
    <orderId>12347</orderId>
    <quantity>1</quantity>
  	<price>28<price>
</Order>
</oracleappshelpOrder>

XQuery FLWOR into HTML

Now , we can use the basic list tag to discplay the data into HTML

<ul>
for $x in doc("order.xml")/oracleappshelpOrder/order/orderId
order by $x
return <li>{$x}</li>
}
</ul> 

<ul>

<li><orderId>12346</orderId></li>

<li> <orderId>12347</orderId> </li>

</ul>

We can also list the actual data without the orderId tag by using the data ($x)

<ul>
for $x in doc("order.xml")/oracleappshelpOrder/order/orderId
order by $x
return <li>{data{$x}}</li>
}
</ul> 

The output will be :

<ul>

<li>12346</li>

<li>12347</li>

</ul>