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:

ForMemberExpr ::= "for" "member" SimpleForBinding ("," SimpleForBinding)* "return" ExprSingle

Here SimpleForBinding takes the same form as in a ForExpr:

"$" VarName "in" ExprSingle

The semantics are as follows:

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?y

returns ("one", "two").

let $json := '[[1,2,3], [4,5,6]]' return for member $A in parse-json($json), $a in $A return $a*2

returns (2, 4, 6, 8, 10, 12).

Note that the result of the expression is a sequence, not an array.