简体   繁体   中英

XSLT compilation error from Java

I have a requirement to load stylesheet into cache. With the following code to compile the XSLT, I get an exception:

javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected.

The XSLT has no errors though attached the XSLT. Below is the XSLT

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
        <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
        <xsl:template match="/">
            <xsl:copy-of select='.'/>
        </xsl:template>
    </xsl:stylesheet>

I have a doubt whether at the statement InputStream stream=ClassLoader.getSystemResourceAsStream(filename); is loading the file or not.

Please help me understand why the compilation is failing; I don't have much experience in Java

    package file;

    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import javax.xml.transform.Source;
    import javax.xml.transform.Templates;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.URIResolver;
    import javax.xml.transform.stream.StreamSource;

    public class XSLT {

        private static Map<String, Templates> templatesMap = new HashMap<String, Templates>();

        public XSLT(){

        }
        public static void main(String[] args) throws Exception{
            XSLT xs=new XSLT();
            System.out.println(xs.readXSLT("/RemoveNamespaces.xslt"));
        }
        public String readXSLT(String filename) throws Exception{
            System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
            //String result="";
            InputStream stream=XSLT.class.getResourceAsStream(filename);
            TransformerFactory xformFactory=TransformerFactory.newInstance();
            xformFactory.setURIResolver(new SimpleURIResolver());

            Source source=new StreamSource(stream);
            try{
                Templates templates=xformFactory.newTemplates(source);
            }
            catch(TransformerConfigurationException  e){
                e.printStackTrace();
            }
            templatesMap.put(filename, xformFactory.newTemplates(source));
            //return templatesMap.get(filename).toString();
            return "Dummy";
        }
    }

    class SimpleURIResolver implements URIResolver {
        public Source resolve(String href, String base) throws TransformerException {
            InputStream stream = ClassLoader.getSystemResourceAsStream(href);
            return new StreamSource(stream);
        }

    }

The first problem is that your code isn't outputting the errors that are reported by the XSLT compiler. This makes it difficult to identify what the errors are. You're only outputting the exception message which says that errors have been reported, and that's not very helpful.

Saxon by default sends the error messages to the standard error output, which is probably sent to some log file somewhere - it all depends on the environment you are running in. You can have the messages sent somewhere else, for example by writing your own ErrorListener and calling xFormFactory.setErrorListener(); or you can change the output destination for Saxon's standard error listener by setting the factory's configuration property http://saxon.sf.net/feature/standardErrorOutputFile to the name of a file where you want the messages written.

Having said that, I strongly suspect that the messages will simply tell you that you aren't picking up the stylesheet file correctly. But it's definitely worth fixing the application so that when you make errors in your stylesheet in future, you can see the messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM