(XQuery/Conditions) Is it possible to declare variables in an if-statement?
Asked Answered
D

3

5

I do not find an example for my problem so here is my question. I get an error that else is an unexpected token in the following example:

let $var1 := 'true'
if ($var1 = 'true') then
    let $var2 := 1
    let $var3 := $var1 + 1
else ()

As you see I want to declare variables if the if-statement is true. Is this possible in XQuery? I just saw examples where the value of just one variable can depends on a condition. The following does more or less the same I want to realize with the code at the beginning.. and it works but it is a little bit confusing in my opinion and actually I don't want the variables to be created if the condition is not true. Furthermore you have to think around the corner when you realize it like that especially when there are more than just 2 variables that depends on each other.

let $var1 := 'true'
let $var2 := if ($var1 = 'true') then (1) else (0)
let $var3 := if ($var2 = 1) then ($var2 + 1) else (0)

So my question is. Is there a prettier solution to realize that than my solution?

Dissimilation answered 16/3, 2017 at 11:31 Comment(0)
E
3

You could avoid using if/else altogether by defining sequences for your possible values, and a predicate that calculates the position() to select the desired value from the sequence:

The following uses number() to evaluate the numeric value of a boolean (0 for false, 1 for true) and selects either the first or the second item in the sequence of values:

let $var1 := 'true'
let $var2 := (0, 1)[number($var1 = 'true') + 1]
let $var3 := (0, $var2 + 1)[number($var2 eq 1) + 1]
return ($var1, $var2, $var3)
Endoblast answered 16/3, 2017 at 15:58 Comment(1)
I'm getting strong APL vibes here. :)Oram
L
2

You could add a return clause to put a full flwor expression inside the condition, e.g. something like this:

let $var := 'true'
if ($var = 'true') then
  let $var2 := 1
  let $var3 := $var1 + 1
  return 0
else ()

But it would be pointless: the binding of $var2 and $var3 would not extend outside of the scope of the then clause.

Langobard answered 16/3, 2017 at 13:52 Comment(0)
P
2

XQuery is a declarative and functional language, which means that variables do not get assigned, but only bound within a certain scope. This is something that should be thought about in term of space, not time, as there is no elapse of time in an XQuery program, like a ticket allows you to visit a museum but not another.

Let clauses are part of FLWOR (acronym for for-let-where-orderby-return) expressions. A variable bound in a let clause can be used in subsequent clauses, up to and including the return clause. As mholstege explains, beyond the return clause, which is required, the variable is not visible any more, like nobody would accept your ticket outside the museum.

Since expressions nest in a "well-parenthesized" way according to the XQuery grammar, any attempt to start a let clause inside an if-then-else expression requires that a return clause be present before the then (or else) expression ends. This means that a variable bound this way will never be visible after this if-then-else expression.

In general, when I program in XQuery (as opposed to, say, Java), I try to remind myself continuously that I have to write down what I want, and resist the temptation to describe how I want it computed.

Having said that, XQuery does have scripting extensions that introduce variable assignments as you describe, but this did not get standardized so far -- also, such a scripting extension should only be used when side effects to the outside world happen, meaning that one needs a notion of time and succeeding snapshots.

Parachronism answered 16/3, 2017 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.