2011-10-11 125 views
3

我已经看到了这个例子贴几次:的XInclude支持6

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> 

我宁可不使用偏移到文档中 - 名字要好得多(如在的第一个版本。 XML)。我试图理解XPointer Framework并且已经使用XPath and XPointer/XPointer Syntax作为参考 - 看来我应该能够使用“简写”指针,但只有在NCName匹配某些ID类型属性时才可以使用。问题是我没有定义“ID类型属性”的DTD或模式。鉴于Java解析器不支持xpointer架构,是我唯一使用索引的选项吗?

回答

3

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

这会让你看起来是下一个问题:元素方案xpointer的语法不正确。有关更多信息,请参阅http://www.w3.org/TR/xptr-element/

+0

谢谢,我知道我在做一些愚蠢的事情。 – foboz