我尝试使用XSLT转换XML文档.作为输入,我有www.wordpress.org XHTML源代码,而XSLT是虚拟示例检索站点的标题(实际上它什么都不做 - 它不会改变任何东西).
我使用的每个API或库,转换大约需要2分钟!如果你看看wordpress.org源代码,你会发现它只有183行代码.正如我用Google搜索,这可能是由于DOM树的构建.无论XSLT多么简单,它总是2分钟 - 所以它确认它与DOM构建有关,但无论如何它不应该花2分钟.
这是一个示例代码(没什么特别的):
  TransformerFactory tFactory = TransformerFactory.newInstance();
   Transformer transformer = null;
   try {
       transformer = tFactory.newTransformer(
           new StreamSource("/home/pd/XSLT/transf.xslt"));
   } catch (TransformerConfigurationException e) {
       e.printStackTrace();
   }
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
   System.out.println("START");
   try {
       transformer.transform(new SAXSource(new InputSource(
           new FileInputStream("/home/pd/XSLT/wordpress.xml"))),
           new StreamResult(outputStream));
   } catch (TransformerException e) {       
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }
   System.out.println("STOP");
   System.out.println(new String(outputStream.toByteArray()));
它位于START和STOP之间,其中java"暂停"2分钟.如果我查看处理器或内存使用情况,则不会增加任何内容.它看起来真的是JVM停止了......
您是否有任何转换长度超过50的XML(这是随机数;))的经验?当我读到XSLT时,总是需要构建DOM树才能完成它的工作.快速转型对我来说至关重要.
提前谢谢,Piotr
示例HTML文件是否使用命名空间?如果是这样,您的XML解析器可能正在尝试从命名空间URI中检索内容(可能是模式).如果每次运行只需要两分钟 - 这可能是一次或多次TCP超时.
您可以通过计算实例化InputSource对象(实际解析WordPress XML的时间)所需的时间来验证这一点,因为这可能是导致延迟的行.在查看您发布的示例文件后,它确实包含一个声明的名称空间(xmlns="http://www.w3.org/1999/xhtml").
要解决此问题,您可以实现自己的功能EntityResolver,从根本上禁用基于URL的解决方案.您可能需要使用DOM -看DocumentBuilder的setEntityResolver方法.
这是使用DOM和禁用解析的示例(注意 - 这是未经测试的):
try {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbFactory.newDocumentBuilder();
    db.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return null; // Never resolve any IDs
        }
    });
    System.out.println("BUILDING DOM");
    Document doc = db.parse(new FileInputStream("/home/pd/XSLT/wordpress.xml"));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(
        new StreamSource("/home/pd/XSLT/transf.xslt"));
    System.out.println("RUNNING TRANSFORM");
    transformer.transform(
            new DOMSource(doc.getDocumentElement()),
            new StreamResult(outputStream));
    System.out.println("TRANSFORMED CONTENTS BELOW");
    System.out.println(outputStream.toString());
} catch (Exception e) {
    e.printStackTrace();
}
如果要使用SAX,则必须使用SAXSource带有XMLReader自定义解析器的a.