Project

Profile

Help

Saxon - XQuery - error() optimized out

Added by Anonymous over 16 years ago

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

Saxon 9.0 ... 9.1 ... XQuery. How can I get calls to error() not optimized out by saxon ? Here's a simplistic example. ------- let $err := if( empty( () ) ) then error( xs:QName( "local:error"), "an error" ) else () return <foo>bar</foo> -------- This never calls error ... This does. --------- let $err := if( empty( () ) ) then error( xs:QName( "local:error"), "an error" ) else () return <foo>bar{$err}</foo> --------- My guess is since in the first case $err is never referenced, then its never evaluated (optimized out). Any suggestions for a coding style that can do non-invasive checks that call error ? I have functions where I'd like to check of a variable is empty and call error(). The only way so far I've figured out how to do it is to include the return of error in the output, but in general I dont want the value of error in the output. I suppose as a last resort I can, but maybe i'm missing something obvious ... like "<!--DO NOT OPTIMIZE OUT CALL->" or saxon:dont-optimize-this-statement Suggestions welcome ! -David


Replies (1)

RE: Saxon - XQuery - error() optimized out - Added by Anonymous over 16 years ago

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

It's an interesting and difficult topic - there's quite a bit of debate in the WG at the moment because some processors are optimizing much more aggressively than others and this affects test results. Unfortunately the exception semantics of the language are not very well formalised. You're right that Saxon will never evaluate a variable that isn't referenced, so calling error() within such a variable initialization achieves nothing. I find that the best approach is usually to concatenate the expression into the result: if( empty( () ) ) then error( xs:QName( "local:error"), "an error") else (), <foo>bar</foo> Technically the spec allows this to be optimized out (the rule is that if there is only one possible value for an expression, ignoring the possibility of errors, then the optimizer is allowed to use that value and ignore the possibility of an error). And some systems will indeed do this. But I think Saxon tries to avoid it: especially if in the example the condition empty(()) is replaced by an expression that can't be evaluated statically.

    (1-1/1)

    Please register to reply