Project

Profile

Help

XPathVariableResolver -a-like for XQuery

Added by Anonymous about 16 years ago

Legacy ID: #4893366 Legacy Poster: David Lee (daldei)

I'm implementing a scripting layer above xquery. I would like to expose all my script variables as XQuery variables to ad-hoc expressions. The only way I've figured out how to do this is for every variable in the environment, do add to head of xquery expression declare variable $varname external ; then in the XQuery processing call setExternalVariable( ... ) I ran across something similar but seems vastly more efficent for XPath, XPathVariableResolver but I cant find a similar thing to XQuery. My use case is that most likely a given expression will not refer to any variables, so I dont want to incur the overhead of declaring and setting potentially hundreds of variables not to be used. Is there any way exposed to "plug into" the variable resolution code of saxon xquery so that I can expose these variables on demand instead of assuming the worse case and predeclare every one of them for every expression ? Thanks for any ideas ! -David Lee


Replies (3)

Please register to reply

RE: XPathVariableResolver -a-like for XQuery - Added by Anonymous about 16 years ago

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

I can't say I'm very keen on the XPath VariableResolver mechanism. It's main fault is that it only binds variable references at run-time, which means there's very little you can do at compile time - you don't even know whether the variable exists, let alone what type it is. I'm not 100% against allowing XQuery variables to be declared programmatically, in the way that namespaces are, by means of a declareVariable() call on the StaticQueryContext object; but I don't think that meets your requirement, which is to avoid declaring variables at all. Frankly, I'm not convinced that's a good idea. It's certainly not high up my priority list of good ideas. The XPath interfaces allow you to do something similar with namespaces, by providing a namespace resolver in the static context object, and it has proved quite troublesome, mainly because there are interfaces that need to know what all the in-scope namespaces are; I think the same problems would occur with variables (for example, consider a debugger). Michael Kay

RE: XPathVariableResolver -a-like for XQuery - Added by Anonymous about 16 years ago

Legacy ID: #4897729 Legacy Poster: David Lee (daldei)

I appreciate the issues you raise. What I was hoping for is a change which would likely make things better for this one use case but worse for a lot of others. Clearly not a great idea ! I would still suggest considering, as you mention, exposing a programatic way to do "declareVariable" on the static query context. This would atleast avoid the parsing overhead of having to generate as text, then pasrse as text a bunch of "declare variable $x external;" statements. Is there a way to inspect a query after its compiled but before its run to determine which variables were actually referenced ? And if so, is it possible to make it a non-error to not set the values of external variables which were not used ? This would make it possible to declare say 100 variables, of which the script might use 2 or 3, then only set those values. I'm also looking at alternatives at the scripting layer (outside the calls to saxon) where I can get information prior to constructing the query on which variables absolutely need to be exposed as xquery variables. Thanks for thinking about this and responding ! -David Lee http://www.xmlsh.org

RE: XPathVariableResolver -a-like for XQuery - Added by Anonymous about 16 years ago

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

>I would still suggest considering, as you mention, exposing a programatic way to do "declareVariable" on the static query context. Yes, I'll consider that. >Is there a way to inspect a query after its compiled but before its run to determine which variables were actually referenced ? Run the explain() method on the XQueryExpression, using an ExpressionPresenter that subclasses the standard ExpressionPresenter, intercepting calls on startElement and emitAttribute to recognize a variableReference element and its name attribute. However, this will also give you references to variables that are locally declared within the query. >is it possible to make it a non-error to not set the values of external variables which were not used ? Use "declare option saxon:default" described at http://www.saxonica.com/documentation/using-xquery/extensions.html Michael Kay

    (1-3/3)

    Please register to reply