XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
alain colletSubject: global variable
Author: alain collet
Date: 21 Mar 2005 01:50 PM
declare variable $XGlobal as xs:integer := 1;

declare function local:ecrireNodeIterXAML($SSTdata, $X, $Y )
{
let $XGlobal := $XGlobal + $X
return $SSTdata
};

let $XGlobal := $XGlobal + 1
let $SSTdata := ""
let $bid := local:ecrireNodeIterXAML($SSTdata, 1, 1 )

I have a need of a real global variable ($XGlobal here) and in this small portion of code, XGlobal in the function is disconnected of the XGlobal value in the main context.

How can I have a real global variable ?
Idea ?
Alain Collet

Postnext
Alberto MassariSubject: global variable
Author: Alberto Massari
Date: 21 Mar 2005 03:07 PM
Hi Alain,

>let $XGlobal := $XGlobal + 1

When you write this expression, you are creating a new XGlobal variable
that has the scope of the FLWOR block, with a value equal to the global
XGlobal variable plus 1.

>How can I have a real global variable ?

In XQuery you cannot change the value of a variable (global or local,
regardless of its type); it's a feature of all "functional languages".
See http://www.w3.org/TR/xquery-semantics/#introduction

"[XPath/XQuery] is a functional language. [XPath/XQuery] is built from
expressions, rather than statements. Every construct in the language
(except for the XQuery query prolog) is an expression and expressions
can be composed arbitrarily. The result of one expression can be used
as the input to any other expression, as long as the type of the result
of the former expression is compatible with the input type of the
latter expression with which it is composed. Another characteristic
of a functional language is that variables are always passed by value,
and a variable's value cannot be modified through side effects."

Hope this helps,
Alberto

Postnext
alain colletSubject: global variable
Author: alain collet
Date: 22 Mar 2005 01:24 PM
(:Thanks for the answer but ... you can see hereafter more information on my problem :)
declare variable $XGlobal as xs:integer := 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 :)

let $XGlobal := $XGlobal + $X
(:here the $XGlobal variable is 3 e:)
return $SSTdata
};

let $XGlobal := $XGlobal + 1
(:here the $XGlobal variable is 2 as I expect and there is only one Global variable in scope:)
let $XGlobal := $XGlobal + 1
(:here the $XGlobal variable is 3 as I expect:)
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) but, when we go into a function, the scope seems not to be preserved and the global variable seems to be reinitialized.
your comment ?
Alain

Posttop
Alberto MassariSubject: global variable
Author: Alberto Massari
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

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.