FLWOR Expressions of XQuery in XML

In this article we will discuss about how to extract the data using FLWOR Expession of XQuery in XML.
  • 2082

Here we will see how to fetch the nodes from the XML document with FLWOR Expression of XQuery using XML. Suppose we want to fetch the book element with the cost less than 400 from the "bookstore.xml" Document then we will use the following Path Expression of XQuery in XML:

doc("bookstore.xml")/Bookshop/Book[Cost<400]/Authorname

The Expression above will extract all the Authorname elements under book elements which is further resides in Bookshop that have a cost element less than 400. We can also write the FLWOR expression to do the same and lets have a look on the following:

for $x in doc("bookstore.xml") /Bookshop/Book
where $x /Cost<400
return $x/Authorname

The Output will be like this:

<Authorname>Nicholas Sparks</Authorname>
<Authorname>ChetanBhagat</Authorname>


If we want we can also sort the output of the XQuery against XML Document by using the following FLWOR Expression of XQuery in XML.
 
for $x in doc("bookstore.xml") /Bookshop/Book
where $x /Cost<400
orderby $x/Authorname
return $x/Authorname

The output will be like this:

<Authorname>ChetanBhagat</Authorname>
<Authorname>Nicholas Sparks</Authorname>

In the above FLWOR Expession of XQuery for clause is used to select all the Book element under the Bookshop element into the variable called $x and where clause is used to select only those Book element with Cost less than 400. Here orderby clause is used to sort the output on the basis of a particular element and return clause is used to specify that what will be returned.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.