Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating variables in XQuery - possible or not?

Tags:

xquery

I'm a little bit confused concerning variable updates in XQuery: On [1] it says:

Variables can't be updated. This means you can't write something like let $x := $x+1. This rule might seem very strange if you're expecting XQuery to behave in the same way as procedural languages such as JavaScript. But XQuery isn't that kind of language, it's a declarative language and works at a higher level. There are no rules about the order in which different expressions are executed (which means that the little yellow triangle that shows the current execution point in the Stylus Studio XQuery debugger and XSLT debugger can sometimes behave in surprising ways), and this means that constructs whose result would depend on order of execution (like variable assignment) are banned.

I'm wondering if there is really no way to reliable update a variable? Maybe I'm just to used to those things in other languages, but I can't really imagine / believe it ;-)

[1] http://www.stylusstudio.com/xquery_flwor.html, second paragraph below the screenshots of the chapter "L is for LET"

UPDATE: I have to add a question to this: Shouldn't it be possible to update an existing variable in an if statement because in this case the order of execution is clear? I guess you just aren't allowed to use something like $x = $x+1 in a loop?

like image 431
stefan.at.wpf Avatar asked Sep 23 '10 21:09

stefan.at.wpf


People also ask

How do you update the value of a variable?

Updating a variable by adding 1 is called an increment; subtracting 1 is called a decrement. Sometimes programmers also talk about bumping a variable, which means the same as incrementing it by 1.

How do I declare a variable in XQuery?

declare variable $x := 7.5; declare variable $x as xs:integer := 7; Functions. “XQuery allows users to declare functions of their own. A function declaration specifies the name of the function, the names and datatypes of the parameters, and the datatype of the result.


1 Answers

You are describing immutability, a feature of functional languages. It's true; once a variable is set to a value, it cannot be set to something else.

Immutability has many benefits. In particular, concurrent programming is made much easier.

In the case of loops, what happens is that a new variable is created each time through the loop, replacing the original one. So immutability still holds. This is explained in detail in the article you linked:

Isn't there a variable being updated when you write something like the following?

for $v in //video
let $x := xs:int($v/runtime) * xdt:dayTimeDuration("PT1M")
return concat($v/title, ": ", 
      hours-from-duration($x), " hour(s) ",
      minutes-from-duration($x), " minutes")

(This query shows the running time of each video. It first converts the stored value from a string to an integer, then multiplies it by one minute (PT1M) to get the running time as a duration, so that it can extract the hours and minutes components of the duration. Try it.)

Here the variable $x has a different value each time around the XQuery for loop. This feels a bit like an update. Technically though, each time round the for loop you're creating a new variable with a new value, rather than assigning a new value to the old variable.

like image 82
Robert Harvey Avatar answered Oct 14 '22 10:10

Robert Harvey