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
Kam ChanSubject: How to access to global variables
Author: Kam Chan
Date: 18 Jun 2009 04:53 PM
Hi,

I need to read in an XML file, set a bunch of commonly used variables, and be able to use these variables from other modules.

To start with, I could define an external variable for the xml file, and then set the list of variables:

(: import other modules :)
import module namespace mod1 = "http://sample.com/mod1" at "mod1.xquery";

(: Defination of External Variables :)
declare variable $input as document-node() external;

(: Defination of Internal Variables :)
declare variable $system := $input/Books;
declare variable $author := $system/author;

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?

How can this be done?

Thanks

Kam

Postnext
(Deleted User) 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.

Alberto

Postnext
Kam ChanSubject: 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?

Postnext
(Deleted User) 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.

Hope this helps,
Alberto

Postnext
Kam ChanSubject: 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 :)
};

Thanks for your reply.

Kam

Posttop
(Deleted User) 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.

For instance, if you have a main module like

-----------
import module namespace p = "http://www.acmme.org" at "module.xquery";

p:concat($p:hello,$p:hello2)
-----------

that imports this module

-----------
module namespace p = "http://www.acmme.org";

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)
};
-----------

and invoke the query using this Java code

xqExpr = xqconnection.prepareExpression(xqueryReader);
xqExpr.executeQuery().writeSequenceToResult(new StreamResult(outWriter));

you can set the $p:hello2 external variable by adding this line before the executeQuery call

tempXQE = bindParameter(xqconnection, xqExpr, new QName("http://www.acmme.org", "hello2"), "a");

Hope this helps,
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.