XQuery FLWOR Expressions

Welcome to The Coding College! In this tutorial, we’ll dive deep into FLWOR expressions in XQuery, the cornerstone of its functionality. FLWOR stands for For, Let, Where, Order by, Return and provides a structured and powerful way to query and transform XML data.

What is a FLWOR Expression?

FLWOR expressions in XQuery allow you to:

  • Iterate through XML data using for.
  • Bind variables to values using let.
  • Filter data with where.
  • Sort the results with order by.
  • Return a transformed or aggregated result.

Think of FLWOR as the XQuery equivalent of a SQL SELECT statement but tailored for hierarchical XML data.

Syntax of FLWOR Expressions

A typical FLWOR expression consists of these clauses:

for $variable in <sequence>
let $var := <expression>
where <condition>
order by <expression>
return <result>

Example 1: Basic FLWOR Expression

Let’s extract the titles of all books in an XML file.

Input XML: books.xml

<library>
  <book>
    <title>Learning XML</title>
    <price>29.99</price>
  </book>
  <book>
    <title>Advanced XSLT</title>
    <price>39.99</price>
  </book>
</library>

XQuery:

for $book in doc("books.xml")/library/book
return $book/title

Output:

<title>Learning XML</title>
<title>Advanced XSLT</title>

Example 2: Filtering Data with where

Let’s retrieve books priced below 35.

XQuery:

for $book in doc("books.xml")/library/book
where $book/price < 35
return $book/title

Output:

<title>Learning XML</title>

Example 3: Binding Values with let

The let clause binds variables to expressions, which can be used later in the query.

XQuery:

for $book in doc("books.xml")/library/book
let $price := $book/price
where $price < 35
return
  <book>
    <title>{ $book/title }</title>
    <price>{ $price }</price>
  </book>

Output:

<book>
  <title>Learning XML</title>
  <price>29.99</price>
</book>

Example 4: Sorting Data with order by

Let’s sort books by their price in ascending order.

XQuery:

for $book in doc("books.xml")/library/book
order by $book/price
return
  <book>
    <title>{ $book/title }</title>
    <price>{ $book/price }</price>
  </book>

Output:

<book>
  <title>Learning XML</title>
  <price>29.99</price>
</book>
<book>
  <title>Advanced XSLT</title>
  <price>39.99</price>
</book>

Example 5: Combining let and order by

You can use let to compute values and then sort based on them.

XQuery:

for $book in doc("books.xml")/library/book
let $discountedPrice := $book/price * 0.9
order by $discountedPrice
return
  <book>
    <title>{ $book/title }</title>
    <discountedPrice>{ $discountedPrice }</discountedPrice>
  </book>

Output:

<book>
  <title>Learning XML</title>
  <discountedPrice>26.991</discountedPrice>
</book>
<book>
  <title>Advanced XSLT</title>
  <discountedPrice>35.991</discountedPrice>
</book>

Example 6: Grouping Data with FLWOR

Let’s group books by category (assuming the XML contains categories).

Input XML:

<library>
  <book>
    <title>Learning XML</title>
    <category>Programming</category>
    <price>29.99</price>
  </book>
  <book>
    <title>Advanced XSLT</title>
    <category>Programming</category>
    <price>39.99</price>
  </book>
  <book>
    <title>Cooking for Beginners</title>
    <category>Cooking</category>
    <price>19.99</price>
  </book>
</library>

XQuery:

for $category in distinct-values(doc("books.xml")/library/book/category)
return
  <category name="{ $category }">
    {
      for $book in doc("books.xml")/library/book
      where $book/category = $category
      return $book/title
    }
  </category>

Output:

<category name="Programming">
  <title>Learning XML</title>
  <title>Advanced XSLT</title>
</category>
<category name="Cooking">
  <title>Cooking for Beginners</title>
</category>

Example 7: Aggregating Data

FLWOR can also be used to perform aggregations. Let’s calculate the total price of books in each category.

XQuery:

for $category in distinct-values(doc("books.xml")/library/book/category)
let $totalPrice := sum(doc("books.xml")/library/book[category = $category]/price)
return
  <category name="{ $category }">
    <totalPrice>{ $totalPrice }</totalPrice>
  </category>

Output:

<category name="Programming">
  <totalPrice>69.98</totalPrice>
</category>
<category name="Cooking">
  <totalPrice>19.99</totalPrice>
</category>

Advanced FLWOR Techniques

Nested FLWOR Expressions

You can nest one FLWOR expression inside another for more complex queries.

Using Multiple for Clauses

FLWOR supports multiple for clauses for iterating over multiple sequences.

Using Predicates in where

Filter data with complex conditions using predicates (e.g., and, or).

Tools for Running FLWOR Queries

  1. BaseX: Lightweight XML database and XQuery processor.
  2. Saxon: High-performance XQuery and XSLT processor.
  3. Altova XMLSpy: Comprehensive XML editing and querying tool.
  4. eXist-db: Open-source XML database with built-in XQuery support.

Conclusion

FLWOR expressions are the heart of XQuery, enabling powerful operations on XML data. From simple filtering to advanced grouping and transformations, FLWOR lets you handle complex XML data with ease.

For more tutorials and examples, visit The Coding College and take your XML and XQuery skills to the next level!

Leave a Comment