Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Xquery Flwor in XML

Xquery Flwor in XML

XQuery FLWOR Expression in XML


What is FLWOR?

FLWOR is the core expression in XQuery used to query and transform XML data. The name FLWOR is an acronym from its key clauses:

  • For

  • Let

  • Where

  • Order by

  • Return


FLWOR Syntax

for $var in expressionlet $var2 := expression2where conditionorder by expression [ascending|descending]return expression3

What Each Clause Does

ClausePurpose
forIterates over a sequence of nodes
letBinds variables to expressions (without iteration)
whereFilters the sequence based on a condition
order bySorts the result according to an expression
returnConstructs the result for each item in the sequence

Example XML

<library>  <book category="fiction">    <title>The Hobbit</title>    <author>J.R.R. Tolkien</author>    <year>1937</year>  </book>  <book category="history">    <title>Sapiens</title>    <author>Yuval Noah Harari</author>    <year>2011</year>  </book>  <book category="fiction">    <title>The Lord of the Rings</title>    <author>J.R.R. Tolkien</author>    <year>1954</year>  </book></library>

Example FLWOR Query: Get titles of fiction books ordered by year

for $b in /library/bookwhere $b/@category = "fiction"order by $b/year ascendingreturn $b/title

Result:

<title>The Hobbit</title><title>The Lord of the Rings</title>

Example with Let Clause

for $b in /library/booklet $title := $b/titlelet $author := $b/authorwhere $b/@category = "fiction"order by $b/year descendingreturn  <bookInfo>    <title>{ $title/text() }</title>    <author>{ $author/text() }</author>  </bookInfo>

Result:

<bookInfo>  <title>The Lord of the Rings</title>  <author>J.R.R. Tolkien</author></bookInfo><bookInfo>  <title>The Hobbit</title>  <author>J.R.R. Tolkien</author></bookInfo>

Summary

  • FLWOR expressions are powerful and flexible for querying XML.

  • Use for to loop over nodes.

  • Use let to bind variables.

  • Use where to filter.

  • Use order by to sort.

  • Use return to produce output.


Need help writing or understanding specific FLWOR queries?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql