我已经看过这个例子好几次了:
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,也一直在使用XPath and XPointer/XPointer Syntax作为引用-似乎我应该能够使用“速记”指针,但前提是NCName必须与某个ID类型的属性相匹配。问题是我没有定义"ID-type属性“的DTD或模式。鉴于Java解析器不支持xpointer模式,我唯一的选择是使用索引吗?
发布于 2011-10-12 01:27:09
xi前缀的名称空间应该是"http://www.w3.org/2001/XInclude“(注意2001年而不是2003年)。有关更多详细信息,请参阅http://www.w3.org/TR/xinclude/。
这似乎是下一个问题:您的元素方案xpointer的语法不正确。有关详细信息,请参阅http://www.w3.org/TR/xptr-element/。
https://stackoverflow.com/questions/7729731
复制相似问题