java 如何解决 javax.xml.transform.TransformerConfigurationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17717652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:46:07  来源:igfitidea点击:

How to resolve javax.xml.transform.TransformerConfigurationException

javaxmlxslt

提问by user2281107

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?

我正在尝试使用 xsl 样式表将 xml 文件转换为 html。请看下面的代码。我已经尝试了很多方法来解决这个问题,但不知何故不能。如果我打开 xml 文件,那么我可以看到所需的输出,但是为什么我无法通过编程看到相同的输出?

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)

错误消息:错误:'Jaxpone.xsl' 致命错误:'无法编译样式表' javax.xml.transform.TransformerConfigurationException:无法在 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.javacrawler:52) .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

XSLT 文件代码

<?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>

回答by Michael Kay

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.

TransformerConfigurationException 通常意味着您的样式表中存在错误。实际错误将通知您的 ErrorListener。您没有提供 ErrorListener,因此它们将转到默认的 ErrorListener,它可能会将消息写入控制台或某个日志文件。

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

尝试从命令行或 IDE 直接运行样式表,直到您知道代码是正确的。