Project

Profile

Help

Can I set the ResourceResolver for fn:transform execution used in XPath evaluation?

Added by Martin Honnen 5 months ago

I am wondering whether I can or cannot expect the ResourceResolver set on XPath evaluation, if the XPath does fn:transform, to be passed on and used during compilation and execution of the stylesheet run by fn:transform.

For the stylesheet compilation, this seems to happen, as https://saxonica.plan.io/projects/saxonmirrorhe/repository/he/revisions/he_mirror_saxon_12_3/entry/src/main/java/net/sf/saxon/functions/TransformFn.java#L620 does xsltCompiler.setResourceResolver(context.getResourceResolver());, but in my tests I find no resource resolution for the execution of the stylesheet.

A sample code to demonstrate the issue is e.g.

package org.example;

import net.sf.saxon.s9api.*;

import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String,String> documents = new HashMap<>();

        documents.put("sample1.xml", "<root>Sample 1</root>");
        documents.put("sample2.xml", "<root>Sample 2</root>");
        documents.put("sheet1.xsl", "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='3.0'><xsl:mode on-no-match='shallow-copy'/><xsl:template match='/'><xsl:next-match/><xsl:copy-of select='doc(\"sample2.xml\")'/></xsl:template></xsl:stylesheet>");

        Processor processor = new Processor(false);

        XPathCompiler xpathCompiler = processor.newXPathCompiler();

        try {
            XPathExecutable xpathExecutable = xpathCompiler.compile("doc('sample1.xml'), doc('sample2.xml'), doc('sheet1.xsl')");

            XPathSelector xpathSelector = xpathExecutable.load();


            xpathSelector.setResourceResolver(resourceRequest -> {
                if (documents.containsKey(resourceRequest.relativeUri)) {
                    return new StreamSource(new StringReader(documents.get(resourceRequest.relativeUri)));
                }
                return null;
            });

            XdmValue result = xpathSelector.evaluate();

            System.out.println(result);

            xpathExecutable = xpathCompiler.compile("transform(map { 'source-node' : doc('sample1.xml'), 'stylesheet-node' : doc('sheet1.xsl'), 'delivery-format' : 'serialized' })");

            xpathSelector = xpathExecutable.load();

            xpathSelector.setResourceResolver(resourceRequest -> {
                if (documents.containsKey(resourceRequest.relativeUri)) {
                    return new StreamSource(new StringReader(documents.get(resourceRequest.relativeUri)));
                }
                return null;
            });

            result = xpathSelector.evaluate();

            System.out.println(result);

        } catch (SaxonApiException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

which errors with e.g.

Error 
  FODC0002  I/O error reported by XML parser processing
  file:/C:/Users/marti/IdeaProjects/XPathCompilerResourceResolverTest1/sample2.xml. Caused
  by java.io.FileNotFoundException:
  C:\Users\marti\IdeaProjects\XPathCompilerResourceResolverTest1\sample2.xml (Das System
  kann die angegebene Datei nicht finden)
  In template rule with match="/" of 
net.sf.saxon.s9api.SaxonApiException: I/O error reported by XML parser processing file:/C:/Users/marti/IdeaProjects/XPathCompilerResourceResolverTest1/sample2.xml
	at net.sf.saxon.s9api.XPathSelector.evaluate(XPathSelector.java:233)
	at org.example.Main.main(Main.java:49)
Caused by: net.sf.saxon.trans.XPathException: I/O error reported by XML parser processing file:/C:/Users/marti/IdeaProjects/XPathCompilerResourceResolverTest1/sample2.xml
	at net.sf.saxon.resource.ActiveSAXSource.deliver(ActiveSAXSource.java:231)
	at net.sf.saxon.resource.ActiveStreamSource.deliver(ActiveStreamSource.java:65)
	at net.sf.saxon.event.Sender.send(Sender.java:105)
	at net.sf.saxon.functions.DocumentFn.makeDoc(DocumentFn.java:311)
	at net.sf.saxon.functions.Doc.call(Doc.java:140)
	at net.sf.saxon.expr.FunctionCall.iterate(FunctionCall.java:562)
	at net.sf.saxon.expr.instruct.CopyOf.processLeavingTail(CopyOf.java:586)
	at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:755)
	at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:392)
	at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:551)
	at net.sf.saxon.trans.XsltController.applyTemplates(XsltController.java:685)
	at net.sf.saxon.s9api.Xslt30Transformer.applyTemplates(Xslt30Transformer.java:425)
	at net.sf.saxon.functions.TransformFn.call(TransformFn.java:827)
	at net.sf.saxon.expr.FunctionCall.iterate(FunctionCall.java:562)
	at net.sf.saxon.sxpath.XPathExpression.iterate(XPathExpression.java:176)
	at net.sf.saxon.s9api.XPathSelector.evaluate(XPathSelector.java:229)
	... 1 more
Caused by: java.io.FileNotFoundException: C:\Users\marti\IdeaProjects\XPathCompilerResourceResolverTest1\sample2.xml (Das System kann die angegebene Datei nicht finden)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileInputStream.<init>(FileInputStream.java:93)
	at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
	at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:624)
	at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:642)
	at net.sf.saxon.resource.ActiveSAXSource.deliver(ActiveSAXSource.java:190)
	... 16 more
I/O error reported by XML parser processing file:/C:/Users/marti/IdeaProjects/XPathCompilerResourceResolverTest1/sample2.xml

fn:transform allows me to set a stylesheet-base-uri but I don't know what to set there if I would want my ResourceResolver of the outer XPath evaluation to handle the sample2.xml request.


Replies (6)

Please register to reply

RE: Can I set the ResourceResolver for fn:transform execution used in XPath evaluation? - Added by Norm Tovey-Walsh 4 months ago

Saxon - Help: RE: Can I set the ResourceResolver for fn:transform execution used in
XPath evaluation?

Martin Honnen

Hey Saxonicans,

any comments/thoughts on this?

I will try to investigate this tomorrow. I thought I’d get to it today,
but…apparently not. :-(

Be seeing you,
norm

--
Norm Tovey-Walsh
Saxonica

Can I set the ResourceResolver for fn:transform execution used in XPath evaluation? - Added by Norm Tovey-Walsh 4 months ago

I am wondering whether I can or cannot expect the ResourceResolver set
on XPath evaluation, if the XPath does fn:transform, to be passed on
and used during compilation and execution of the stylesheet run by
fn:transform.

It seems like a reasonable expectation.

What’s happening today is that the fn:transform function creates a new
Xslt30Transformer to do the transformation but it doesn’t set the
resource resolver on that transformer to the resolver from the
XPathContext passed in. I think it should.

See issue #6230

Be seeing you,
norm

--
Norm Tovey-Walsh
Saxonica

RE: Can I set the ResourceResolver for fn:transform execution used in XPath evaluation? - Added by Martin Honnen 4 months ago

Wow, Norm, already fixed, with a unit case, all, as far as the saxonica.plan.io site tells, within a few minutes. You are really doing a good "Michael Kay" impression if you keep up that responsiveness and speed... :)

RE: Can I set the ResourceResolver for fn:transform execution used in XPath evaluation? - Added by Norm Tovey-Walsh 4 months ago

Wow, Norm, already fixed, with a unit case, all, as far as the saxonica.plan.io site tells,
within a few minutes. You are really doing a good "Michael Kay" impression if you keep up that
responsiveness and speed... :)

Thanks. :-) I aim to please. And I really appreciate your attention to
detail when providing reproducible tests for issues!

Be seeing you,
norm

--
Norm Tovey-Walsh
Saxonica

    (1-6/6)

    Please register to reply