سؤال

I've stuck on a problem with Saxon 9.5 HE and I haven't found a solution for days. Maybe it is something obvious and easy, but I just can't see it.

What I'm trying to do is to XSL transform XML by specifying a custom URIResolver.

If I run this, I get my XSL files loaded and the transformation takes place producing a valid result.

try {
  //tfactory.setURIResolver(new DOTUriResolver());          
  Transformer transformer  = tfactory.newTransformer(xslSource);            
if (transformer!=null)
  transformer.transform(xmlSource, new StreamResult(System.out));
} 
catch (Exception e) {
  System.err.println(e.getMessage());
}

If I comment out

tfactory.setURIResolver(new DOTUriResolver());          

And provide a very simple URIResolver that just prints out the values of base and href:

@Override
public Source resolve(String href, String base) throws XPathException {
  System.out.println( " resolve: " + base + " | " + href);
  return;
}

I get errors when creating the transformer object.

javax.xml.transform.TransformerConfigurationException: XML-22000: (Fatal Error) Error while parsing XSL file

Looking in what is printed by my URIResolver, I see that the base parameter is always the same and has the value of the root (first) XSL file. I would expect that base changes based on the current XSL file that contains the xsl:import or xsl:include tags (or document()), but that stays the same for all files.

This is the code that creates the xslSource and sets the systemId:

StreamSource source = new StreamSource(file);
String systemID = file.toURI().toURL().toString();
source.setSystemId(systemID);

Do you have any ideas why is this happening? This was used to be working in the past with previous versions of Saxon (9.1).

Any help would be much appreciated.

EDIT: This is my URI resolver (full code):

package com.mekon.xproc;

import javax.xml.transform.Source;
import javax.xml.transform.URIResolver;
import net.sf.saxon.trans.XPathException;

public class DOTUriResolver implements URIResolver{

    @Override
    public Source resolve(String href, String base) throws XPathException {     
        System.out.println( " resolve: " + base + " | " + href);
        return null;
    }   
}

Now, if I use it I get the problem described above. Base will remain the same for every call, the same value as for the first source. If I don't call it, then the URIs are resolved with no problems.

هل كانت مفيدة؟

المحلول 2

The problem is fixed if I use:

TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);

Obviously, it is a classpath issue that I need to investigate, presumably Xalan is referenced too.

Many thanks for your time.

نصائح أخرى

Need to see the full code of your URIResolver.

I'm not sure why you're setting both a File and a URI on the Source object. The code of the constructor is:

       public StreamSource(File f) {
144        //convert file to appropriate URI, f.toURI().toASCIIString()
145        //converts the URI to string as per rule specified in
146        //RFC 2396,
147        setSystemId(f.toURI().toASCIIString());
148    }

When Saxon reads a stylesheet module from a Source supplied by the URIResolver, it will use the SystemId property of that Source object as the base URI for that stylesheet module (assuming of course there are no external entities, xml:base attributes, etc to confuse things.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top