xpathxquery

Is it safe to define a variable and use it in an other `:=` before the `return`?


I remember reading somewhere that XPath could potentially process the components of an expression in //; so my question is: Can I define a variable and use it in an other variable definition of the same let expression before the return? For example:

let
    $a := //abc,
    $x := $a/xyz
return
    $x

Or do I have to write, to be on the safe side:

let $a := //abc return
let $x := $a/xyz return
$x

Solution

  • The rules depend on which version of XPath you are using.

    XPath 1.0 and 2.0 do not have a "let" expression.

    XPath 3.0 and 3.1 allow let $a :=1, $b := 2 return $a+$b and they allow let $a :=1 return let $b := 2 return $a+$b but they do not allow let $a :=1 let $b := 2 return $a+$b. The draft XPath 4.0 proposal allows all these forms (as do all versions of XQuery).