Question

I am trying to create HTML files using XSLT, I have used xml file and xsl files to create HTML file. Here some other xsl files which are located in same location are included in xsl file by using <xsl:include href="temp.xsl"/>.

Here Xsl files are located in "D:/XSL_Folder/" path. I am running Main.java file which is located in D:/Workspace/Webapp_Project/ path.

When i try to create HTML files by using passing "D:/XSL_Folder/root.xsl" and "D:/XML_Folder/data.xml" files to Main.java as arguments, I am getting following error while creating Templates.

Templates lTemplates = TransformerFactory.newInstance().newTemplates(new StreamSource(new FileInputStream(lFileXSL)));

ERROR: 'D:\Workspace\Webapp_Project\temp.xsl (The system cannot find the file specified)'
FATAL ERROR: 'Could not compile stylesheet'
12:20:07 ERROR f.s.t.v.v2.dao.impl.DocUnitDaoImpl - Error while creating a new XslTransformerGetter. The path to the XSL may be wrong. javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:885) ~[na:1.7.0_13]

In error report we can see that parser is checking included xsl file in project path (D:\Workspace\Webapp_Project), not in the path where root.xsl file is located (D:/XSL_Folder/).

Can anyone suggest me why parser searching xsl file in project folder in the path where root.xsl file is located and how to fix this problem?

Code I'm using to create HTML file by using XSL and XML file :

public static void simpleTransform(InputStream lXmlFileStream, File lXSLFile,  
    StreamResult lHtmlResult, Map<String, String> lArguments) {  
        TransformerFactory tFactory = TransformerFactory.newInstance();  
        try {  
            Transformer transformer =  
            tFactory.newTransformer(new StreamSource(lXSLFile));    
            for (Entry<String, String> lEntrie : lArguments.entrySet()) {
                transformer.setParameter(lEntrie.getKey(), lEntrie.getValue());
            }
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(new StreamSource(lXmlFileStream), lHtmlResult);
        } 
        catch (Exception e) {  
          e.printStackTrace();  
        }  
}
Was it helpful?

Solution

You have tagged the question "saxon", and you have said you are using XSLT 2.0, but the error messages show that you are using Xalan. If you specifically want to use Saxon then the best way is to avoid using the JAXP classpath search and instantiate Saxon directly - in place of TransformerFactory.newInstance(), use new net.sf.saxon.TransformerFactory().

Supplying a File as the argument to StreamSource ought to be OK; but I would like to see how the File lXSLFile object is created. My suspicion would be that you have done something like new File ("root.xsl") and it has been resolved relative to the current directory.

OTHER TIPS

You may try to use <xsl:include href="resolve-uri('temp.xsl')"/> instead of <xsl:include href="temp.xsl"/> to avoid this problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top