I am trying to use Saxon HE from C# and the XQuery processor to process an input XML document as follows (input document is req.XML and XQuery string is req.XQuery):
Processor processor = new Processor();
var documentBuilder = processor.NewDocumentBuilder();
documentBuilder.BaseUri = new Uri("file://");
var xdmNode = documentBuilder.Build(new StringReader(req.XML));
XQueryCompiler compiler = processor.NewXQueryCompiler();
compiler.BaseUri = "file:///C:/temp/";
compiler.DeclareNamespace("saxon", "http://saxon.sf.net/");
XQueryExecutable exp = compiler.Compile(req.XQuery);
XQueryEvaluator eval = exp.Load();
eval.ContextItem = xdmNode;
var results = new XdmDestination();
eval.Run(results);
XmlXQueryResponse output = new XmlXQueryResponse();
output.XMLResult = results.XdmNode.OuterXml;
And here is the XQuery query I am testing with:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
And here is the XML file I am testing with: https://www.w3schools.com/xml/books.xml
However, when I run this, I get the following error:
Saxon.Api.DynamicError: 'Exception thrown by URIResolver'
This error is thrown by the line eval.Run(results).
If I remove the line compiler.BaseUri = "file:///C:/temp/"; I get the same error.
I struggling to understand why it is complaining about BaseUri, since I am new to Saxon HE.
Any idea why this might be happening? Any help with this would be greatly appreciated.
books.xml? Why do you try to setContextItembut in the XQuery use no context item.or why don't you simply use the pathfor $x in /bookstore/book? I think you get the error asdoc("books.xml")fails. Of course if you want to load the file directly from the server then you can just usedoc('https://www.w3schools.com/xml/books.xml'). - Martin Honnen