X包括 Java 6 中的支持

发布于 2024-12-09 10:16:32 字数 2222 浏览 3 评论 0原文

我已经多次看到这个例子:

public class XIncludeTest {

  public static void main(String[] args) throws Exception {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setXIncludeAware(true);
      DocumentBuilder docBuilder = factory.newDocumentBuilder();
      System.out.println("isXIncludeAware " + docBuilder.isXIncludeAware());
      Document doc = docBuilder.parse(args[0]);

      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");

      //initialize StreamResult with File object to save to file
      StreamResult result = new StreamResult(new StringWriter());
      DOMSource source = new DOMSource(doc);
      transformer.transform(source, result);

      String xmlString = result.getWriter().toString();
      System.out.println(xmlString);    
    }
}

我将这个简单的 xml 文件传入:

a.xml
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2003/XInclude">
  <xi:include href="b.xml" xpointer="element(/b/c)"/>
  <xi:include href="b.xml" xpointer="element(/b/d)"/>
</a>

b.xml
<?xml version="1.0" encoding="UTF-8"?>
<b>
  <c>1</c>
  <d>2</d>
</b>

我得到的只是 a.xml 的内容,如上所述 - 没有包含 b.xml 的任何部分。我已经尝试了 xpointer 语法的无数变体,但没有成功。然而,我已经能够通过 XML::LibXML 在 perl 中工作,但我需要它在 Java 中工作。

我没有得到什么?

好的,现在我已经将 xml 文件更新为有效的内容:

a.xml
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="b.xml" xpointer="element(/1/1)"/>
  <xi:include href="b.xml" xpointer="element(/1/2)"/>
</a>

我不想在文档中使用偏移量 - 名称要好得多(就像 a.xml 的第一个版本一样)。我试图理解 XPointer Framework 并一直在使用 < a href="http://commons.oreilly.com/wiki/index.php/XPath_and_XPointer/XPointer_Syntax" rel="nofollow">XPath 和XPointer/XPointer Syntax 作为参考 - 看来我应该能够使用“速记”指针,但前提是 NCName 与某些 ID 类型属性匹配。问题是我没有定义“ID 类型属性”的 DTD 或模式。鉴于 Java 解析器不支持 xpointer 模式,我是否只能选择使用索引?

I've seen this example posted several times:

public class XIncludeTest {

  public static void main(String[] args) throws Exception {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setXIncludeAware(true);
      DocumentBuilder docBuilder = factory.newDocumentBuilder();
      System.out.println("isXIncludeAware " + docBuilder.isXIncludeAware());
      Document doc = docBuilder.parse(args[0]);

      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");

      //initialize StreamResult with File object to save to file
      StreamResult result = new StreamResult(new StringWriter());
      DOMSource source = new DOMSource(doc);
      transformer.transform(source, result);

      String xmlString = result.getWriter().toString();
      System.out.println(xmlString);    
    }
}

I pass this simple xml file in:

a.xml
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2003/XInclude">
  <xi:include href="b.xml" xpointer="element(/b/c)"/>
  <xi:include href="b.xml" xpointer="element(/b/d)"/>
</a>

b.xml
<?xml version="1.0" encoding="UTF-8"?>
<b>
  <c>1</c>
  <d>2</d>
</b>

And all I get back out is the contents of a.xml, as above - no part of b.xml was included. I've tried endless variations on the xpointer syntax to no avail. I have, however, been able to get things to work in perl via XML::LibXML but I need this to work in Java.

What am I not getting?

OK, now I've updated my xml file to something that works:

a.xml
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="b.xml" xpointer="element(/1/1)"/>
  <xi:include href="b.xml" xpointer="element(/1/2)"/>
</a>

I'd rather not use offsets into the document - names are much better (like in the first version of a.xml). I'm trying to understand XPointer Framework and have been using XPath and XPointer/XPointer Syntax as a reference as well - it seems I should be able to use a "shorthand" pointer but only if the NCName matches some ID-type attribute. Problem is that I don't have a DTD or schema that defines an "ID-type attribute". Given that the Java parser doesn't support the xpointer schema, is my only option to use indexes?

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

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

发布评论

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

评论(1

溺ぐ爱和你が 2024-12-16 10:16:32

xi 前缀的命名空间应为“http://www.w3.org/2001/XInclude”(注意 2001 而不是 2003)。有关更多详细信息,请参阅 http://www.w3.org/TR/xinclude/

这会让您遇到下一个问题:元素方案 xpointer 的语法不正确。请参阅 http://www.w3.org/TR/xptr-element/更多信息。

The namespace for the xi prefix should be "http://www.w3.org/2001/XInclude" (note 2001 not 2003). See http://www.w3.org/TR/xinclude/ for more details.

That gets you to what seems to be the next problem: the syntax of your element scheme xpointer is incorrect. See http://www.w3.org/TR/xptr-element/ for more information.

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