Java 6 中默认支持 xinclude 吗?

发布于 2024-11-03 10:46:42 字数 389 浏览 0 评论 0原文

我在我的应用程序中看到,解析的 XML 文件中的 xinclude 在我的 Java XSLT 转换中不起作用。

然而,尽管我这样做:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setXIncludeAware(true);

我没有专门将变压器工厂设置为 System.getProperty("javax.xml.transform.TransformerFactory") 返回“null”。

我的问题:默认的 Java(1.6 或 6)是否支持 xinclude 或者我是否必须添加替代的 XSLT 解析器,例如 Apache Xerces?

I see in my application that xinclude inside my parsed XML file does not work within my Java XSLT conversion.

However, although I do:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setXIncludeAware(true);

I'm not specifically setting the transformer factory as System.getProperty("javax.xml.transform.TransformerFactory") returns "null".

My question: does the default Java (1.6 or 6) support xinclude or do I have to add an alternative XSLT parser such as Apache Xerces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-11-10 10:46:42

根据规范,自 Java 1.5 以来就一直支持(5)。我相信 XInclude 支持依赖于 命名空间感知,出于向后兼容性的原因,默认情况下该功能处于关闭状态。

public class XIncludeDemo {

    private static final String XML = "<?xml version=\"1.0\"?>\n"
            + "<data xmlns=\"foo\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n"
            + "<xi:include href=\"include.txt\" parse=\"text\"/>\n"
            + "</data>\n";

    private static final String INCLUDE = "Hello, World!";

    public static void main(String[] args) throws Exception {
        // data
        final InputStream xmlStream = new ByteArrayInputStream(XML
                .getBytes("UTF-8"));
        final InputStream includeStream = new ByteArrayInputStream(INCLUDE
                .getBytes("UTF-8"));
        // document parser
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setXIncludeAware(true);
        factory.setNamespaceAware(true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        if (!docBuilder.isXIncludeAware()) {
            throw new IllegalStateException();
        }
        docBuilder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                if (systemId.endsWith("include.txt")) {
                    return new InputSource(includeStream);
                }
                return null;
            }
        });
        Document doc = docBuilder.parse(xmlStream);
        // print result
        Source source = new DOMSource(doc);
        Result result = new StreamResult(System.out);
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(source, result);
    }

}

According to the spec, support has been there since Java 1.5 (5). I believe XInclude support relies on namespace awareness, which is turned off by default for backwards compatibility reasons.

public class XIncludeDemo {

    private static final String XML = "<?xml version=\"1.0\"?>\n"
            + "<data xmlns=\"foo\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">\n"
            + "<xi:include href=\"include.txt\" parse=\"text\"/>\n"
            + "</data>\n";

    private static final String INCLUDE = "Hello, World!";

    public static void main(String[] args) throws Exception {
        // data
        final InputStream xmlStream = new ByteArrayInputStream(XML
                .getBytes("UTF-8"));
        final InputStream includeStream = new ByteArrayInputStream(INCLUDE
                .getBytes("UTF-8"));
        // document parser
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setXIncludeAware(true);
        factory.setNamespaceAware(true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        if (!docBuilder.isXIncludeAware()) {
            throw new IllegalStateException();
        }
        docBuilder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                if (systemId.endsWith("include.txt")) {
                    return new InputSource(includeStream);
                }
                return null;
            }
        });
        Document doc = docBuilder.parse(xmlStream);
        // print result
        Source source = new DOMSource(doc);
        Result result = new StreamResult(System.out);
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(source, result);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文