Project

Profile

Help

Variables in static context unavailable

Added by Anonymous almost 18 years ago

Legacy ID: #3741568 Legacy Poster: dali1138 (dali1138)

Hi all, I'm attempting to use Saxon programmatically as an Xpath 2.0 engine. I'm passing a variable to the StaticQueryContext used to compile my expression (e.g. $var1/foo/bar), but I'm getting the error message: Variable $var1 has not been declared. Here's my code: GlobalVariableDefinition var = new GlobalVariableDefinition(); var.setVariableName(name); Document doc = (new Builder()).build(new File("C:\myXml.xml"); // mStaticContext is a StaticQueryContext instance DocumentWrapper dw = new DocumentWrapper(doc, doc.getBaseURI(), mStaticCtx.getConfiguration()); var.setValueExpression(new SingletonNode(dw)); var.setRequiredType(SequenceType.SINGLE_NODE); mStaticCtx.declareVariable(var); // fails on next line XQuery xq = new XQuery("$var1/foo/bar", null, mStaticCtx, null); The question is: shouldn't variables in the static context be available during compilation of an expression? Thanks!


Replies (5)

Please register to reply

RE: Variables in static context unavailable - Added by Anonymous almost 18 years ago

Legacy ID: #3741612 Legacy Poster: Michael Kay (mhkay)

The XQuery API in Saxon doesn't allow variables to be declared externally to the query. I suggest that if you're doing XPath, it would be better to use the XPath API, which does allow this. StaticQueryContext unfortunately contains rather a mixture of methods designed for public use and methods designed for internal use.

RE: Variables in static context unavailable - Added by Anonymous almost 18 years ago

Legacy ID: #3741650 Legacy Poster: dali1138 (dali1138)

What is the reason for not allowing externally declared variables? If XPath supports it and XQuery is a superset of XPath, doesn't it follow that this functionality should be available?

RE: Variables in static context unavailable - Added by Anonymous almost 18 years ago

Legacy ID: #3741665 Legacy Poster: Michael Kay (mhkay)

The rationale is that XQuery allows you to declare the variable within the query prolog, and XPath doesn't. The assumption is that because the prolog is available, it's a good idea to use it. In fact the XQuery language spec does allow variables to be referenced that aren't declared in the prolog, but I'm not sure it's a good idea. The XQJ API (XQuery API for Java) has now been published and in future it's likely that I'll be following that.

RE: Variables in static context unavailable - Added by Anonymous almost 18 years ago

Legacy ID: #3741811 Legacy Poster: dali1138 (dali1138)

Could you please share your thoughts on why it may not be a good idea to allow variables declared outside the prolog? Please consider the following scenario: A BPEL engine is using Saxon to evaluate Xpath expressions used in BPEL assign statements. Any necessary variables are already declared in the BPEL document. If these variables cannot be passed to the static context, the BPEL engine has to modify the Xpath expression used in the assign to "redeclare" the variables in XQuery syntax. Or is there an alternative way to specify these variables without modifying the Xpath expression?

RE: Variables in static context unavailable - Added by Anonymous almost 18 years ago

Legacy ID: #3741895 Legacy Poster: Michael Kay (mhkay)

The scenario you are describing is XPath, not XQuery. As I think I said, Saxon's XPath API does allow an XPath expression to reference variables that are defined by the host language or API: XPath is designed to be embedded in that sort of way. In XQuery, it seems to me, there is no good reason why a variable used in the query should not be declared (as an external variable) in the prolog.

    (1-5/5)

    Please register to reply