Project

Profile

Help

Support #6505

closed

Unable to resolve nested xsl:include statements in Java code

Added by Mark Dunn about 1 month ago. Updated about 1 month ago.

Status:
Closed
Priority:
Low
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2024-08-13
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
12
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

Description

I am writing a Java application to execute XSLT.

The top level stylesheet includes another stylesheet, which includes another. All using relative URLs.

In the Java code I have tried setting the System ID of the XSLT resource when building my XsltExecutable:

public void setXSLT(String filepath) {
        ClassLoader cl = getClass().getClassLoader();
        InputStream is = cl.getResourceAsStream(filepath);
        URL url = cl.getResource(filepath);
        this.xsltStream = new StreamSource(is);
        xsltStream.setSystemId(url.toExternalForm());
        this.xsltExec = this.xsltCompiler.compile( this.xsltStream);
}

To resolve xsl:include/@href in the XSLT I wrote an implementation of URIResolver:

public class BasicURIResolver implements URIResolver {

    @Override
    
    public Source resolve(String href, String base) throws TransformerException {
        System.out.println("[BasicURIResolver] href = " + href);
        System.out.println("[BasicURIResolver] base = " + base);
        ClassLoader cl = this.getClass().getClassLoader();
        try {
            InputStream is = cl.getResourceAsStream(href);
            URL url = cl.getResource(href);
            Source source = new StreamSource(is);
            source.setSystemId(url.toExternalForm());
            return source;  
        }
        catch (Exception ex) {
            System.out.println("ERROR: (resolving " + href + ") " + ex.getMessage());
        }
        return null;
  }
}

This is registered in the Saxon config file:

<global 
        uriResolver="com.oup.bits2word.xml.BasicURIResolver"
    />

This works for the first level of xsl:include, but at the next level down the URL is null and I get the errors:

ERROR: (resolving log4xslt.xsl) Cannot invoke "java.net.URL.toExternalForm()" because "url" is null
ERROR: (resolving common.templates.xsl) Cannot invoke "java.net.URL.toExternalForm()" because "url" is null
ERROR: (resolving common.variables.xsl) Cannot invoke "java.net.URL.toExternalForm()" because "url" is null

The base URI is supplied to the resolve() method as shown in the console log:

[BasicURIResolver] href = lib/utilities/log4xslt/log4xslt.config.xsl
[BasicURIResolver] base = file:/C:/SVN/software/General/HTMLtools/BITS2WORD/BITS2Word/target/classes/BITS2GenXHTML.xslt
[BasicURIResolver] href = log4xslt.xsl
[BasicURIResolver] base = file:/C:/SVN/software/General/HTMLtools/BITS2WORD/BITS2Word/target/classes/lib/utilities/log4xslt/log4xslt.config.xsl
ERROR: (resolving log4xslt.xsl) Cannot invoke "java.net.URL.toExternalForm()" because "url" is null
...

I'm guessing this is a problem in my Java code, or an error in my Saxon configuration, rather than any kind of bug in Saxon but I'm unable to see how to fix it.


Files

debug.zip (891 Bytes) debug.zip Mark Dunn, 2024-08-13 14:49
xslt-include-resources.png (16.7 KB) xslt-include-resources.png Mark Dunn, 2024-08-13 14:49

Please register to edit this issue

Also available in: Atom PDF