positional variables and extension functions
Added by Anonymous over 19 years ago
Legacy ID: #3199629 Legacy Poster: marcvc (marcvc)
Michael, Consider the following Java program import net.sf.saxon.value.Value; import net.sf.saxon.Configuration; import net.sf.saxon.om.SequenceIterator; import net.sf.saxon.query.DynamicQueryContext; import net.sf.saxon.query.StaticQueryContext; import net.sf.saxon.query.XQueryExpression; public class PositionalVar { public static Value foo(Value val){ return val; } public static void main(String[] args) throws Exception { Configuration config = new Configuration(); StringBuffer sb = new StringBuffer(); sb.append("declare namespace ext = 'java:PositionalVar';"); sb.append("for $v1 at $i in (1,2) order by $v1 return (ext:foo($i))"); StaticQueryContext staticContext = new StaticQueryContext(config); XQueryExpression exp = staticContext.compileQuery(sb.toString()); DynamicQueryContext dynamicContext = new DynamicQueryContext(config); SequenceIterator i = exp.iterator(dynamicContext); while (i.next() != null) System.out.println(i.current().getStringValue()); } } It outputs twice "2", where I expected "1" and "2". Removing the exyension function or the orderby in the query makes it work. Thanks, Marc
Replies (3)
Please register to reply
RE: positional variables and extension functions - Added by Anonymous over 19 years ago
Legacy ID: #3199630 Legacy Poster: marcvc (marcvc)
Forgot to mention that I'm using Saxon 8.4 Thanks, Marc
RE: positional variables and extension functi - Added by Anonymous over 19 years ago
Legacy ID: #3200437 Legacy Poster: Michael Kay (mhkay)
Thanks. In fact it doesn't have to be an extension function, any function will do, e.g. declare function local:val($i as xs:integer) { $i+0 }; for $v1 at $i in (1,2) order by $v1 return local:val($i) It's a bug in lazy evaluation: one that I suspected (see the TODO associated with the class PositionBinding in ForExpression.java) but which I hadn't managed to prove or disprove. It's basically happening because positional variables aren't held on the stack and therefore aren't copied as part of a Closure. I think the cleanest answer is probably to put positional variables on the stack in the same way as other variables. This will be too difficult a change to produce as a patch. Michael Kay
RE: positional variables and extension functi - Added by Anonymous over 19 years ago
Legacy ID: #3200478 Legacy Poster: Michael Kay (mhkay)
I've fixed this by allocating a slot number to positional variables. Michael Kay
Please register to reply