如何告诉xalan不要验证使用“文档”检索的XML。功能? [英] how can I tell xalan NOT to validate XML retrieved using the "document" function?

查看:121
本文介绍了如何告诉xalan不要验证使用“文档”检索的XML。功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天甲骨文决定取消java.sun.com一段时间。这对我来说很麻烦,因为xalan试图验证一些XML但无法检索属性.dtd。

Yesterday Oracle decided to take down java.sun.com for a while. This screwed things up for me because xalan tried to validate some XML but couldn't retrieve the properties.dtd.

我正在使用xalan 2.7.1运行一些XSL转换,我不希望它验证任何东西。
因此尝试像这样加载XSL:

I'm using xalan 2.7.1 to run some XSL transforms, and I don't want it to validate anything. so tried loading up the XSL like this:

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
XMLReader rdr = spf.newSAXParser().getXMLReader();      
Source xsl = new SAXSource(rdr, new InputSource(xslFilePath));  
Templates cachedXSLT  = factory.newTemplates(xsl);
Transformer transformer = cachedXSLT.newTransformer();         
transformer.transform(xmlSource, result);  

在XSL本身,我这样做:

in the XSL itself, I do something like this:

  <xsl:variable name="entry" select="document(concat($prefix, $locale_part, $suffix))/properties/entry[@key=$key]"/>

此代码检索的XML在顶部具有以下定义:

The XML this code retrieves has the following definition at the top:

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="...

尽管上面的java代码指示解析器为了不验证,它仍然向java.sun.com发送请求。虽然java.sun.com不可用,但这使得转换失败并显示消息:

Despite the java code above instructing the parser to NOT VALIDATE, it still sends a request to java.sun.com. While java.sun.com is unavailable, this makes the transform fail with the message:

 Can not load requested doc: http://java.sun.com/dtd/properties.dtd

如何让xalan停止尝试验证从document函数加载的XML?

How do I get xalan to stop trying to validate the XML loaded from the "document" function?

推荐答案

文档提到解析器可能会读取DTD,即使没有验证,因为可能需要使用DTD来解析(扩展)实体。

The documentation mentions that the parser may read the DTDs even if not validating, as it may become necessary to use the DTD to resolve (expand) entities.

我无法控制XML文档,我无法使用不修改XML的选项

Since I don't have control over the XML documents, nont's option of modifying the XML was not available to me.

我设法通过破坏解析器来关闭尝试拉入DTD文件,如下所示。

I managed to shut down attempts to pull in DTD documents by sabotaging the resolver, as follows.

我的代码使用DocumentBuilder返回Document(= DOM)但是根据OP示例的XMLReader也有一个方法 setEntityResolver 所以相同的技术应该适用于。

My code uses a DocumentBuilder to return a Document (= DOM) but the XMLReader as per the OP's example also has a method setEntityResolver so the same technique should work with that.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false); // turns off validation
factory.setSchema(null);      // turns off use of schema
                              // but that's *still* not enough!
builder = factory.newDocumentBuilder();
builder.setEntityResolver(new NullEntityResolver()); // swap in a dummy resolver
return builder().parse(xmlFile); 

现在,这是我的假解析器:它会返回一个空的InputStream,无论它被问到什么。

Here, now, is my fake resolver: It returns an empty InputStream no matter what's asked of it.

/** my resolver that doesn't */
private static class NullEntityResolver implements EntityResolver {

    public InputSource resolveEntity(String publicId, String systemId) 
    throws SAXException, IOException {
        // Message only for debugging / if you care
        System.out.println("I'm asked to resolve: " + publicId + " / " + systemId);
        return new InputSource(new ByteArrayInputStream(new byte[0]));
    }

}






或者,您的假解析器可以返回作为本地资源或其他任何内容读取的实际文档流。


Alternatively, your fake resolver could return streams of actual documents read as local resources or whatever.

这篇关于如何告诉xalan不要验证使用“文档”检索的XML。功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆