How to resolve javax.xml.transform.TransformerConf

2019-06-24 10:14发布

I am trying to convert an xml file to html using xsl stylesheets. Please see the code below. I have tried many ways to resolve the issue but somehow cant. If I open the xml file then I am able to see the desired output, but why am i not able to see the same through programming?

Error Message: ERROR: 'Jaxpone.xsl' FATAL ERROR: 'Could not compile stylesheet' javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:885) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:671) at crawler.JAXPExamples.basic(JAXPExamples.java:52) at crawler.JAXPExamples.main(JAXPExamples.java:40)

Please see the code below

package crawler;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.SAXException;


public class JAXPExamples {


    public static void main(String argv[])
        throws TransformerException, TransformerConfigurationException,
               IOException, SAXException, ParserConfigurationException,                 
               FileNotFoundException
        {
        try {
         URL xmlURL = new URL("file://Jaxpone.xml");
         String xmlID = xmlURL.toString();
         URL xslURL = new URL("file://Jaxpone.xsl");


         String xslID = xslURL.toString();
     //
         System.out.println("--- basic ---");
         basic(xmlID, xslID);
         System.out.println();

      } catch(Exception err) {
        err.printStackTrace();
      }
   }
      public static void basic(String xmlID, String xslID)
      throws TransformerException, TransformerConfigurationException
   {
      TransformerFactory tfactory = TransformerFactory.newInstance();
      Transformer transformer = tfactory.newTransformer(new StreamSource(xslID));

      StreamSource source = new StreamSource(xmlID);
      transformer.transform(source, new StreamResult(System.out));
   }

}

XSLT file code

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="title">
        <h2><b><xsl:value-of select="."/></b></h2><br />
    </xsl:template>

    <xsl:template match="pub_date">
        <h5><xsl:value-of select="."/></h5><br />
    </xsl:template>

        <xsl:template match="section">
        <p><b><xsl:value-of select="."/></b></p><br />
    </xsl:template>

        <xsl:template match="author">
        <p><b><xsl:value-of select="."/></b></p><br />
    </xsl:template>

        <xsl:template match="link">
        <p><xsl:value-of select="."/></p><br />
    </xsl:template>

        <xsl:template match="description">
        <p><xsl:value-of select="."/></p><br />
    </xsl:template>

        <xsl:template match="body">
       <p><xsl:value-of select="."/></p><br />
    </xsl:template>

    <xsl:template match="/">
        <html>
        <body>
        <xsl:apply-templates/>
        </body>
        </html>
</xsl:template>

</xsl:stylesheet>

标签: java xml xslt
1条回答
我只想做你的唯一
2楼-- · 2019-06-24 10:41

A TransformerConfigurationException generally means there is an error in your stylesheet. The actual errors will have been notified to your ErrorListener. You didn't supply an ErrorListener so they will go to the default ErrorListener, which will probably write messages to the console, or to some log file.

Try running the stylesheet direcly from the command line or from an IDE until you know the code is correct.

查看更多
登录 后发表回答