.NET XQuery: Unable to pass an external variable to XQueryEvaluator
Added by Thomas Florkiewicz over 7 years ago
Hello,
I am writing an application that will take complex types and use them as input into XQuery expressions. For this purpose, I am trying to pass an external variable into the XQueryEvaluator.
I didn't see a sample of this in the .NET samples, and the Java samples provided for this case use types that don't seem to be present in the .NET API.
Is it at all possible to do this using the .NET API?
A simple type I am trying to pass in as external variable:
public class Foo
{
public int First { get; set; }
public override string ToString()
{
return string.Format("First: {0}", First);
}
}
And this is the code I am using:
static void Main(string[] args)
{
Processor processor = new Processor();
XQueryCompiler compiler = processor.NewXQueryCompiler();
compiler.BaseUri = "file:///dummy/base/uri";
compiler.ErrorList = new ArrayList();
compiler.DeclareNamespace("spreadtrading", "http://saxon.sf.net/");
XQueryExecutable exp = compiler.Compile("declare variable $payload external; {$payload}");
XQueryEvaluator eval = exp.Load();
var foo = new Foo{First = 7};
var pv = XdmAtomicValue.WrapExternalObject(foo);
eval.SetExternalVariable(new QName("payload"), pv);
Serializer qout = new Serializer();
qout.SetOutputProperty(Serializer.METHOD, "xml");
qout.SetOutputProperty(Serializer.INDENT, "yes");
qout.SetOutputStream(new FileStream("testoutput2.xml", FileMode.Create, FileAccess.Write));
eval.Run(qout);
}
When I run it, the line above: var pv = XdmAtomicValue.WrapExternalObject(foo); throws exception: Unable to cast object of type 'Saxon.Api.XdmValue' to type 'Saxon.Api.XdmAtomicValue'.
Considering the exception, I also tried two other things before posting here: 1) XdmValue.Wrap(Sequence foo) but I am unsure of how to convert my type to a Sequence, which seems to be an internal Saxon type.
new XdmValue(foo) with foo being a List, and type Foo inheriting from abstract class XdmItem But that throws a NPE during eval.Run(quot)
System Info Saxon version="9.7.0.15" .NET Framework 4.0
Replies (4)
Please register to reply
RE: .NET XQuery: Unable to pass an external variable to XQueryEvaluator - Added by Thomas Florkiewicz over 7 years ago
Note: also tried with saxon version "9.8.0.2" / .net4.5 with same issues
RE: .NET XQuery: Unable to pass an external variable to XQueryEvaluator - Added by O'Neil Delpratt over 7 years ago
Hi Thomas,
Thats for reporting this issue. It is a bug. I am creating a bug issue to keep track of its fix. Please see: https://saxonica.plan.io/issues/3359
RE: .NET XQuery: Unable to pass an external variable to XQueryEvaluator - Added by O'Neil Delpratt over 7 years ago
Further to my last comment. We will be updating the API to handle external object. This will be available in the next release. In the meanwhile as a work-around you can do the following:
var foo = new Foo { First = 7 };
var pv = new net.sf.saxon.value.ObjectValue(foo);
eval.SetExternalVariable(new QName("payload"), XdmValue.Wrap(pv));
RE: .NET XQuery: Unable to pass an external variable to XQueryEvaluator - Added by Thomas Florkiewicz over 7 years ago
Hi O'Neil,
Thanks for looking at this and the fast response!
I tried the work-around and it seems to work so far. Will keep an eye out on the new version.
Thanks! Tom
Please register to reply