Using relative URIs in the functions collection or uri-collection in XPath or XSLT code compiled from a string
Added by Martin Honnen almost 5 years ago
I am struggling to understand how relative URIs in calls to the functions uri-collection
and collection
in XPath or XSLT code loaded from a string are supposed to be resolved and how they are resolved in Saxon (testing with Saxon 9.9.1.6):
The spec text of both uri-collection
(https://www.w3.org/TR/xpath-functions/#func-uri-collection) and collection
says about relative URIs:
If the value of $arg is a relative xs:anyURI, it is resolved against the value of the base-URI property from the static context.
The spec text of the doc
function (https://www.w3.org/TR/xpath-functions/#func-doc) says:
If $uri is a relative URI reference, it is resolved relative to the value of the static base URI property from the static context.
In terms of the spec, is there any difference between "the base-URI property from the static context" and "the static base URI property from the static context"?
In terms of Saxon, why does Java code
Processor processor = new Processor(false);
XPathCompiler xpathCompiler = processor.newXPathCompiler();
XPathExecutable xpathExecutable = xpathCompiler.compile("count(doc('sample-10.xml'))");
XPathSelector xpathSelector = xpathExecutable.load();
System.out.println(xpathSelector.evaluateSingle());
xpathExecutable = xpathCompiler.compile("count(collection('?select=sample-*.xml'))");
xpathSelector = xpathExecutable.load();
System.out.println(xpathSelector.evaluateSingle());
that outputs 1
for the first evaluateSingle
, meaning without setting any base URI the processor uses the current working directory for it where the file sample-10.xml
exists, throw an exception "no base URI is available" on the second call?
The exception is
Exception in thread "main" net.sf.saxon.s9api.SaxonApiException: Relative URI passed to document() function (); but no base URI is available
at net.sf.saxon.s9api.XPathSelector.evaluateSingle(XPathSelector.java:172)
at saxontest.SaxonTest.main(SaxonTest.java:28)
Caused by: net.sf.saxon.trans.XPathException: Relative URI passed to document() function (); but no base URI is available
at net.sf.saxon.functions.DocumentFn.resolveURI(DocumentFn.java:400)
at net.sf.saxon.resource.CatalogCollection.catalogContents(CatalogCollection.java:134)
at net.sf.saxon.resource.CatalogCollection.getResourceURIs(CatalogCollection.java:53)
at net.sf.saxon.resource.CatalogCollection.getResources(CatalogCollection.java:61)
at net.sf.saxon.functions.CollectionFn.getSequenceIterator(CollectionFn.java:137)
at net.sf.saxon.functions.CollectionFn.call(CollectionFn.java:239)
at net.sf.saxon.expr.FunctionCall.iterate(FunctionCall.java:532)
at net.sf.saxon.expr.parser.Evaluator$7.evaluate(Evaluator.java:239)
at net.sf.saxon.expr.SystemFunctionCall.evaluateArguments(SystemFunctionCall.java:446)
at net.sf.saxon.expr.FunctionCall.iterate(FunctionCall.java:530)
at net.sf.saxon.sxpath.XPathExpression.evaluateSingle(XPathExpression.java:207)
at net.sf.saxon.s9api.XPathSelector.evaluateSingle(XPathSelector.java:166)
How would I set a base URI for the collection
or uri-collection
function?
Replies (2)
RE: Using relative URIs in the functions collection or uri-collection in XPath or XSLT code compiled from a string - Added by Martin Honnen almost 5 years ago
If I explicitly set
xpathCompiler.setBaseURI((new File(".")).toURI());
then all three XPath evaluations work.
Is that as intended, that the doc
call somehow infers the base URI from the working directory but the collection/uri-collection
calls don't do that?
RE: Using relative URIs in the functions collection or uri-collection in XPath or XSLT code compiled from a string - Added by Michael Kay almost 5 years ago
is there any difference between "the base-URI property from the static context" and "the static base URI property from the static context"?
No. The correct term is "Static Base URI".
We've been historically inconsistent about what to do when no static base URI is known. There are some APIs such as XQueryCompiler.compile()
where we explicitly say that the base URI defaults to the current working directory, but I'm reluctant to do this everywhere because I think it's potentially a security problem.
It's particularly awkward when you run from SEF files that aren't in the same location as the original stylesheet. There are some words in the spec that acknowledge this problem, but they don''t really provide a solution.
Please register to reply