One thing that I really think XQuery needs is the ability to specify a default value for an external variable. I wrote a workaround for our XQuery servlet awhile back but, since I needed to update our XQs with the newest release of our native XML database (it supports a newer version of the spec), I’m looking at the problem again.
Currently, defining a variable that will be passed into the XQuery from an external source is pretty easy:
declare variable $xsl as external;
The problem is… what if you don’t want to pass in default values for every variable (wouldn’t it be nice if you could define them in the XQ itself)? On my first time through this problem I received a sensible (though verbose) suggestion to define another variable:
declare variable $xsl as external; declare variable $_xsl := if ($xsl) then $xsl else 'default.xsl';
That looks good, looks like it should work, but with my particular native XML database (and perhaps others?) you still need to pass in an empty value for the variable itself. If you don’t have xquery.xq?xsl= then it will fail with an ‘unknown external variable xsl’ exception. The person who made this suggestion, told me empty values could be passed in the code that calls the XQ.
But, I don’t think I should have to tell the XQ what its variables are. In my case, where I have a single XQuery servlet that evaluates all *.xq, this means I need to keep track of all the relevant variables for any given XQ (which is what I currently do by parsing the XQ to see what external variables it defines (this could also be done easier with an XQuery Java API)). It seems like a lot of work though (even if it is cached) compared with what would be a nice, simple solution:
declare variable $xsl external 'default';
Or, something like that… this way, when an external variable isn’t passed in there would be some way to specify what the default value that be should be used is. I don’t know if something like this is in the works for XQuery but it seems like it would make sense to me. Or, perhaps, there is another way around this problem that I’m missing(?)

Posts