Empty sequence as xpath variable value
Added by Anonymous over 15 years ago
Legacy ID: #6904158 Legacy Poster: Chris Khan (chriskhan)
When trying to evaluate xpath which references a variable with an empty sequence as a value, I get an error saying that the variable value is null. I spent a good amount of time researching the problem and was hoping for some quick help. I'm using Saxon-B v9.1.0.6 in a .NET 2.0 application on WinXP SP3. The following is the type of behavior I'm referring to: Saxon.Api.Processor proc = new Processor(); {Saxon.Api.Processor} Implementation: {net.sf.saxon.Configuration@bd0a2} IsSchemaAware: false ProductTitle: "SAXON 9.1.0.6 from Saxonica" ProductVersion: "9.1.0.6" SchemaManager: null XmlResolver: {System.Xml.XmlUrlResolver} XmlVersion: 1 XPathCompiler comp = proc.NewXPathCompiler(); {Saxon.Api.XPathCompiler} BackwardsCompatible: false BaseUri: "" comp.DeclareVariable(new QName("","test")); Expression has been evaluated and has no value XPathSelector sel = comp.Compile("for $var in $test return 1").Load(); {Saxon.Api.XPathSelector} ContextItem: '(XPathSelector sel = comp.Compile("for $var in $test return 1").Load();).ContextItem' threw an exception of type 'System.InvalidCastException' sel.SetVariable(new QName("","test"), XdmEmptySequence.INSTANCE); Expression has been evaluated and has no value XdmValue val = sel.Evaluate(); 'sel.Evaluate()' threw an exception of type 'net.sf.saxon.trans.XPathException' base {javax.xml.transform.TransformerException}: {"No value has been supplied for variable $test"} XdmItem item = sel.EvaluateSingle(); 'sel.EvaluateSingle()' threw an exception of type 'java.lang.AssertionError' base {java.lang.Error}: {"Value of variable is undefined (null) Variable test at 1 of null"} XdmValue val = sel.Evaluate(); A local variable named 'val' is already defined in this scope val = sel.Evaluate(); 'sel.Evaluate()' threw an exception of type 'net.sf.saxon.trans.XPathException' base {javax.xml.transform.TransformerException}: {"No value has been supplied for variable $test"} sel = comp.Compile("$test").Load(); {Saxon.Api.XPathSelector} ContextItem: '(sel = comp.Compile("$test").Load()).ContextItem' threw an exception of type 'System.InvalidCastException' sel.SetVariable(new QName("","test"), XdmEmptySequence.INSTANCE); Expression has been evaluated and has no value item = sel.EvaluateSingle(); 'sel.EvaluateSingle()' threw an exception of type 'java.lang.AssertionError' base {java.lang.Error}: {"Value of variable is undefined (null) Variable test at 1 of null"} Any advice, whether on proper XPath or Saxon usage, would be appreciated. This works fine in the context of XSLT processing, so I have assumed that it's a Saxon-specific issue. Chris
Replies (2)
RE: Empty sequence as xpath variable value - Added by Anonymous over 15 years ago
Legacy ID: #6930455 Legacy Poster: Michael Kay (mhkay)
Thanks for pointing out this bug. I've logged it as bug #2707393, and have put a patch in Subversion (though not very thoroughly tested). Meanwhile, rather than using XdmEmptySequence to represent an empty sequence, I suggest creating an XdmValue that wraps, say, an empty list of integers. Michael Kay
RE: Empty sequence as xpath variable value - Added by Anonymous over 15 years ago
Legacy ID: #6967450 Legacy Poster: Chris Khan (chriskhan)
I had originally used an XPath workaround that made me cringe, but I didn't want to waste any more time trying to figure out the "proper" workaround. After reading your suggestion, I restored my XPath and included this line of code: if (val is XdmEmptySequence) val = XdmValue.Wrap(new net.sf.saxon.value.IntegerRange(1, 0)); Is there a way to do this without having to reference the Saxon and/or OpenJDK libraries? Is there a way to trigger this underlying ValueRepresentation with XPath alone? (This additional question was partially an excuse to say thanks for everything that you do from xml standards development to quick Saxon help forum responses.)
Please register to reply