2
votes

The output of

<output>{
  let $m := 2
  let $n := "Hello"
  let $s := $m+1
  return (
    <m>{ $m }</m>,
    <n>{ $n }</n>,
    <s>{ $s }</s>
  )
}</output>           

is

<output>
  <m>2</m>
  <n>Hello</n>
  <s>3</s>
</output>

Why is the output for the following query identical?

<output>{
  for $m in 2
  for $n in "Hello"
  for $s in $m+1
  return (
    <m>{ $m }</m>,
    <n>{ $n }</n>,
    <s>{ $s }</s>
  )
}</output>
1

1 Answers

4
votes

If the expression that is bound to for yields a single item as result (such as in your example), it is equivalent to using let. If it returns multiple items, things look different:

  • With for, each item will be bound to the variable one by one.
  • With let, all items (i.e., the full sequence) will be bound once.

You will notice the difference if you e.g. replace 2 by (1,2,3) in your query and run it again.