For-member expressions
The for member
expression is a Saxon extension to XPath 3.1 syntax introduced in Saxon 10. It allows
convenient iterative processing of the members of an array.
For example:
for member $m in [(3,5,6), (8,12)] return sum($m)returns the sequence (14, 20)
.
A ForMemberExpr
is a new kind of ExprSingle
; this means it can be used anywhere a ForExpr
or LetExpr
is allowed. The syntax is:
Here SimpleForBinding
takes the same form as in a ForExpr
:
The semantics are as follows:
- Each expression in a
SimpleForBinding
must evaluate to a single array. - The expression
for member $x in X, $y in Y return R
is equivalent tofor member $x in X return for member $y in Y return R
. - The expression
for $x in X return R
binds the variable$x
to each member of the arrayX
in turn, evaluatesR
with this variable binding in scope, and returns the sequence-concatenation of all the evaluations ofR
, in order.
The for member
expression is currently available only as a free-standing expression; it cannot be
used as a clause within an XQuery FLWOR expression.
Some further examples:
let $json := '[{"x":1, "y":"one"}, {"x":2, "y":"two"}]' return for member $map in parse-json($json) return $map?yreturns ("one", "two")
.
returns (2, 4, 6, 8, 10, 12)
.
Note that the result of the expression is a sequence, not an array.