|
top
|
Subject: global variable Author: (Deleted User) Date: 22 Mar 2005 01:40 PM
|
Hi Alain,
what you are seeing is what I explained with the sentence "you are
creating a new XGlobal variable that has the scope of the FLWOR block".
>declare variable $XGlobal as xs:integer := 1;
This creates a global variable with value 1
>declare function local:ecrireNodeIterXAML($SSTdata, $X, $Y )
>{
>let $XGlobal := $XGlobal + $X
>(:here the $XGlobal variable is 2 It seems that the
>$XGlobal is reinitialiszed to 1 e:)
>(: I expected to have a 3+1 = 4 here and not 2 :)
You are creating a new local variable named XGlobal that is equal to
the value of the global variable plus 1, i.e. 2
>let $XGlobal := $XGlobal + $X
>(:here the $XGlobal variable is 3 e:)
Here you are still inside the FLWOR block, so you are creating a new
local variable XGlobal that will be in scope until the end of the FLWOR
block, and its value is that value of the local XGlobal previously
defined plus 1, that is 3.
> return $SSTdata
>};
>
>let $XGlobal := $XGlobal + 1
>(:here the $XGlobal variable is 2 as I expect and there is
>only one Global variable in scope:)
By chance, as you are creating a new local variable named XGlobal with
the value of the global variable XGlobal plus 1, that is 2.
>let $XGlobal := $XGlobal + 1
>(:here the $XGlobal variable is 3 as I expect:)
But you are creating yet another local variable, taking the value of
the previous local variable XGlobal plus 1.
>let $SSTdata := ""
>let $bid :=
>local:ecrireNodeIterXAML($SSTdata, 1, 1 )
>return $bid
>
>Conclusion
>We can change the content of global variable in a scope
>(main program) [...]
>your comment ?
You are not changing the value of the global variable, as XQuery cannot
change the value of any variable (local or global). In the LET statement
you are simply creating new local variables that hides previously
defined variables having the same name. But as soon as you step out
of their scope, XGlobal will be referring to the global variable, that
is always equal to 1.
Hope this clears the issue,
Alberto
|
|
|
|