0
votes

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.

2
Can you show us a minimal but complete sample to allow us to reproduce the problem? How does the Xquery code look, how the input string? - Martin Honnen
@MartinHonnen Thanks for replying. I added this information - the XQuery and the input XML string into the question. - A X
Where have you placed the file books.xml? Why do you try to set ContextItem but in the XQuery use no context item . or why don't you simply use the path for $x in /bookstore/book? I think you get the error as doc("books.xml") fails. Of course if you want to load the file directly from the server then you can just use doc('https://www.w3schools.com/xml/books.xml'). - Martin Honnen

2 Answers

1
votes

If you want to load the file from the server use

for $x in doc("https://www.w3schools.com/xml/books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

https://xqueryfiddle.liberty-development.net/eiZQaEM

For your query, if you parse the XML yourself with DocumentBuilder and set it as the context item, you can just use for $x in /bookstore/book, you don't need the doc function call.

0
votes

If the file actually exists at file:///C:/temp/books.xml then I would expect your code to succeed.

If the file doesn't exist at that location, then I would expect a different error: an FODC0005 or FODC0002 that clearly tells you the file is not found.

If the location file:///C:/temp/ doesn't exist, then I can see things getting more confused; perhaps we should be producing better diagnostics in this case.

However, you haven't given enough information for us to tell which of these cases applies.