Syntax of FLWOR expressions

A FLWOR expression is composed of the following clauses, some of which are optional: for, let, where, order by, and return.

Read syntax diagramSkip visual syntax diagramfor clauselet clausewhereExpressionorder by,ExpressionascendingdescendingreturnExpression
for clause
Read syntax diagramSkip visual syntax diagramfor ,$ VariableNameat$ PositionalVariableNameinExpression
let clause
Read syntax diagramSkip visual syntax diagramlet ,$ VariableName:=Expression
for
The keyword that begins a for clause. A for clause iterates over the result of Expression and binds VariableName to each item that is returned by Expression.
let
The keyword that begins a let clause. A let clause binds VariableName to the entire result of Expression.
VariableName
The name of the variable to bind to the result of Expression.
PositionalVariableName
The name of an optional variable that is bound to the position within the input stream of the item that is bound by each iteration of the for clause.
Expression
Any XQuery expression. If the expression includes a top-level comma operator, then the expression must be enclosed in parentheses.
where
The keyword that begins a where clause. A where clause filters the tuples of variable bindings that are generated by the for and let clauses.
order by
The keywords that begin an order by clause. An order by clause specifies the order in which values are processed by the return clause.
ascending
Specifies that ordering keys are processed in ascending order.
descending
Specifies that ordering keys are processed in descending order.
return
The keyword that begins a return clause. The expression in the return clause is evaluated once for each tuple of bound variables that is generated by the for, let, where, and order by clauses. If the return clause contains a non-updating expression, the FLWOR expression is a non-updating expression. The results of all of the evaluations of the return clause are concatenated into a single sequence, which is the result of the FLWOR expression.
If the return clause contains an updating expression, the FLWOR expression is an updating expression. An updating FLWOR expression must be specified within the modify clause of a transform expression. The result of the updating FLWOR expression is a list of updates. The containing transform expression performs the updates after merging them with other updates returned by other updating expressions within the modify clause of the transform expression.