The first two expressions are functionally equivalent. The difference is the use of FLWOR vs. XPath to select your sequence.
In the second example, you are calling sum()
on each item of the sequence ($c/hours
), instead of on the sequence itself:
let $bd := doc("document")
return sum(
for $c in $bd//module[@id]
where $c/hours>100
return $c/hours)
You could also use XPath:
let $bd := doc("document")
let $c := $bd//module[@id][hours>100]
return sum($c/hours)
Or similarly assign the result of the FLWOR to a variable and sum that:
let $bd := doc("document")
let $c :=
for $m in $bd//module[@id]
where $m/hours>100
return $m/hours
return sum($c)