FLWORExpr

Iterate over sequences of items.


Syntax

FLWORExpr

graphics/FLWORExpr.png

ForClause LetClause WhereClause OrderByClause Expr

Description

A FLWOR expression iterates over a sequence of items and binds variables that can be used in the scope of the current expression. If the item sequence is empty, the result of the FLWOR expression is an empty sequence. A FLWOR expression consists of one or more for and let clauses in any combination, followed by an optional where clause, an optional order by clause and a return clause. Briefly, these clauses are interpreted as follows:

  • A for clause binds one or more variables to each value of the result of the following expression.

  • A let clause binds one or more variables to the complete result of the expression.

  • A where clause retains only those intermediate results that satisfy the following condition.

  • An order by clause can reorder the intermediate results.

  • A return clause evaluates the following expression and returns the result.

Examples

  • Retrieve a list of book titles along with a current list number:

    for $a at $i in input()/bib/book
    return (<BookNo>{ $i }</BookNo>, $a/title)

    A positional variable is used for creating the current list number, see the description of the ForClause for details.

  • Join Operations
    1. Since a for clause creates tuples of variable bindings from the Cartesian product of the sequences that the expressions evaluate to, a cross join is straightforward:

      for $i in (1, 2), $j in (3, 4)
      return
       <tuple>
         <i>{ $i }</i>
         <j>{ $j }</j>
       </tuple>

      In SQL contexts, this concept corresponds to the CROSS JOIN expression.

    2. Select all books for which a review exists, with all authors, title and the review text:

      for    $b in input()/bib/book,
             $a in input()/reviews/entry
      where  $b/title = $a/title
      order by $b/title
      return
        <book>
          { $b/author }
          { $b/title }
          { $a/review }
        </book>

      This FLWOR expression is an example of an equijoin: from both doctypes bib and reviews only those tuples are retained that satisfy the equality condition in the where clause. The tuples are then sorted according to the book title in ascending order. See also the result in the XQuery 4 User Guide. In SQL contexts, this concept corresponds to the JOIN ... ON expression.

  • A FLWOR expression can often simply be a replacement for a path expression using predicates (filter):

    input()/bib/book[author/last = "Stevens"]

    can also be written as:

    for $a in input()/bib/book
    where $a/author/last = "Stevens"
    return $a

    Although this query expression is longer than the first one, it generally performs better and it is recommended to use FLWOR expressions instead of path expressions with filters. See also the section Efficient Queries: XQuery in the Performance Guide.

Related Syntax Constructs

The following construct(s) refer to this construct:

This construct is also related to the following construct(s):

ForClause LetClause OrderByClause WhereClause