These variables can be referenced by functions in this main file, but how do I access these variables from another module, like mod1?
Alternatively, I tried to put the external variable and the variables in another module, but external variable can't be declared (or initialized) in modules. Right?
Subject:How to access to global variables Author:(Deleted User) Date:19 Jun 2009 11:03 AM
Hi Kam,
a proper design would be placing in a module a set of functions that communicate with the main module only through function arguments. Otherwise you are going to have the same problem when you try to access the same input file form different modules.
Subject:How to access to global variables Author:Kam Chan Date:19 Jun 2009 05:32 PM
That is the intent. Currently the code was written (by another team) as everything in a single file. I wanted to break in up into modules so that it can be more maintainable.
But since the current code is written this way, and I don't want to change the functionality (yet), so I don't want to rewrite everything. To keep everything as close to as it is functioning, but to introduce the modularization, I need to be able to have modules accessing variables that is read in by the "main". Or to move the read-in external variable and the global variables (and functions) into another module.
Thus, I asked how this can be done.
Is there any solution to this? Or is it simply not doable in xQuery?
Subject:How to access to global variables Author:(Deleted User) Date:23 Jun 2009 04:31 AM
Hi Kam,
the only thing that could be possible is to have a module declare an external variable, and define it when running the query (note: I haven't tested it, but the XQuery specs don't list this scheme neither as valid or prohibited).
This variable could then be seen by the main module, but not from other included modules.
Unfortunately Stylus Studio doesn't inspect imported modules for such declaration, and will not help you defining them in the Scenario Dialog property pages, so such a query could only be run from a command line.
Subject:How to access to global variables Author:Kam Chan Date:23 Jun 2009 09:33 AM
Alberto,
These xQueries will be used inside a server that has a custom implementation of the DataDirect xQuery processor. So I don't think running from a command line would be solution for that environment.
I guess the title of my question of accessing global variables is not the perfect description of what I was trying to do.
I think I was looking for an Object-Oriented solution in xQuery. I wanted to have an module (similar to a class) that manages the data and provide a bunch of getters to accessing the values. If I can initialize the data being encapsulated by this module, that should work. Your suggestion of declaring an external variable is close to what I think would work.
But as you stated, and I had previously tried, that I can't set the value to the external variable in a module. I also tried to have an "init()" method to set the root doc, and then the getters would just retrieve values from the root doc. But the init method can't set the root doc variable inside the module, because xQuery won't allow the value of a declared variable to be changed, once it is set.
module xyz
declare variable xyz:root as document-node();
declare function xyz:init(data as element())
{
xyz := data (: can't really do this :)
};
Subject:How to access to global variables Author:(Deleted User) Date:26 Jun 2009 06:20 AM
Hi Kam,
when I was suggesting to use a command line, I was referring to the fact that Stylus Studio doesn't expose a UI to set variables that are defined in a module, so you have to manually set them.
In your case, as you are running DDXQ from a server application, you are already in control of how DDXQ is invoked, and can plug the appropriate code.
declare variable $p:hello as xs:string := "Hello, World!";
declare variable $p:hello2 as xs:string external;
declare function p:concat($st1 as xs:string, $st2 as xs:string) as xs:string
{
if(string-length($st1) > string-length($st2)) then
concat($st1, $st2)
else
concat($st2, $st1)
};
-----------