Project

Profile

Help

Problem with calling Java from XQuery?

Added by Anonymous over 16 years ago

Legacy ID: #4519790 Legacy Poster: warty (warthog2040)

Hi, I'm writing some XQuery scripts to process XML but need to call Java from the scripts. I found some strange behaviour. I'll try to explain it using examples. This code to set the year to 1980 DOESN'T work; it still returns the the current year... It doesn't work in my program (using Saxon8.9B) nor in OxygemXML 8.2 (with default Saxon as the query parser). It does however work in eXist... declare namespace gc = "java:java.util.GregorianCalendar"; let $cal := gc:new() let $y := gc:set($cal, 1, 1980) let $x := gc:get($cal, 1) return $x I experimented in OxygenXML and found that changing the return a little DOES work: declare namespace gc = "java:java.util.GregorianCalendar"; let $cal := gc:new() let $y := gc:set($cal, 1, 1980) let $x := gc:get($cal, 1) return ($y, $x) It seems that the call to set the year to 1980 isn't actually executed unless the return value (even it it's a void) is used in the final result. Some sort of lazy evaluation? Can anyone shed some light on this? Thanks.


Replies (2)

RE: Problem with calling Java from XQuery? - Added by Anonymous over 16 years ago

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

Invoking external methods that have side-effects is not a good idea and the results in general are not well-defined. If a variable isn't used in a Query, then Saxon won't evaluate it. Even if you force the variable to be evaluated by referencing it as in your return($x,$y) version of the query, the order of evaluation of different variables isn't defined, so you can't be sure whether the $x value is evaluated before or after the call on set(). The safest thing to do here is to write a Java method that performs your three calls (in the right order), and then call that method from the query. Michael Kay http://www.saxonica.com/

RE: Problem with calling Java from XQuery? - Added by Anonymous over 16 years ago

Legacy ID: #4520561 Legacy Poster: warty (warthog2040)

Ok, thanks Michael.

    (1-2/2)

    Please register to reply