xml – 如何使用带有Xinclude的xpointer来引用元素
发布时间:2020-12-16 05:33:13 所属栏目:百科 来源:网络整理
导读:我想合并2个具有相同结构的 XML文件来制作一个.例如; Test1.xml ?xml version="1.0" encoding="UTF-8"?ns:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:TestNamespace" xsi:schemaLocation="urn:Test.Namespace Test1.xsd" ns
我想合并2个具有相同结构的
XML文件来制作一个.例如;
Test1.xml <?xml version="1.0" encoding="UTF-8"?> <ns:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:TestNamespace" xsi:schemaLocation="urn:Test.Namespace Test1.xsd" > <ns:element1 id="001"> <ns:element2 id="001.1" order="1"> <ns:element3 id="001.1.1" /> </ns:element2> <ns:element2 id="001.2" order="2"> <ns:element3 id="001.1.2" /> </ns:element2> </ns:element1> </ns:Root> 和Test2.xml <?xml version="1.0" encoding="UTF-8"?> <ns:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:TestNamespace" xsi:schemaLocation="urn:Test.Namespace Test1.xsd" > <ns:element1 id="999"> <ns:element2 id="999.1" order="1"> <ns:element3 id="999.1.1" /> </ns:element2> </ns:element1> </ns:Root> 创造 TestOutput.xml <?xml version="1.0" encoding="UTF-8"?> <ns:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:TestNamespace" xsi:schemaLocation="urn:Test.Namespace Test1.xsd" > <ns:element1 id="001"> <ns:element2 id="001.1" order="1"> <ns:element3 id="001.1.1" /> </ns:element2> <ns:element2 id="001.2" order="2"> <ns:element3 id="001.1.2" /> </ns:element2> </ns:element1> <ns:element1 id="999"> <ns:element2 id="999.1" order="1"> <ns:element3 id="999.1.1" /> </ns:element2> </ns:element1> </ns:Root> 即一个XML文件,其中包含每个元素. 我在StackOverflow上找到了一个有用的问题,并提出了这个问题; 的merge.xml <?xml version="1.0"?> <ns:Root xmlns:xi="http://www.w3.org/2003/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:TestNamespace"> <xi:include href="Test1.xml" parse="xml" xpointer="element(//ns:Root/ns:element1)" /> <xi:include href="Test2.xml" parse="xml" xpointer="element(//ns:Root/ns:element1)" /> </ns:Root> 我通过这样做运行(我需要使用xmllint,因为参与的原因)
但这不起作用,它抱怨各种thiongs,似乎与xpointer有关. parser error : warning: ChildSeq not starting by /1 Merge.xml:7: element include: XInclude error : XPointer evaluation failed: #element(//ns:Root/ns:element1) Merge.xml:7: element include: XInclude error : could not load Test1.xml,and no fallback was found parser error : warning: ChildSeq not starting by /1 Merge.xml:9: element include: XInclude error : XPointer evaluation failed: #element(//ns:Root/ns:element1) Merge.xml:9: element include: XInclude error : could not load Test2.xml,and no fallback was found <?xml version="1.0"?> <ns:Root xmlns:xi="http://www.w3.org/2003/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:TestNamespace"> <xi:include href="Test1.xml" parse="xml" xpointer="element(//ns:Root/ns:element1)"/> <xi:include href="Test2.xml" parse="xml" xpointer="element(//ns:Root/ns:element1)"/> </ns:Root> 如果我省略了Merge.xml中的xpointer属性,那么我会得到一些合理的输出,但它做的不仅仅包括我想要的元素. 有人可以就xpointer我做错了什么提出一些建议吗? 谢谢你的反对.
我已经涉足了这个问题,并在网上发现了大量的例子,表明我正在做的事情是正确的.这是一个正在运行的版本……
<?xml version="1.0"?> <Root xmlns:xi="http://www.w3.org/2003/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="http://testurl.com/now"> <xi:include href="Test1.xml" xpointer="xmlns(ns=http://testurl.com/now)xpointer(/ns:Root/ns:element1)" parse="xml" /> <xi:include href="Test2.xml" xpointer="xpointer(//Root/element1)" parse="xml" /> </Root> 此示例使用具有名称空间的Test1.xml版本和不具有名称空间的Test2.xml. 输出现在看起来像这样…. <?xml version="1.0"?> <Root xmlns:xi="http://www.w3.org/2003/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="http://testurl.com/now"> <ns:element1 xmlns:ns="http://testurl.com/now" id="001"> <ns:element2 id="001.1" order="1"> <ns:element3 id="001.1.1"/> </ns:element2> <ns:element2 id="001.2" order="2"> <ns:element3 id="001.1.2"/> </ns:element2> </ns:element1><ns:element1 xmlns:ns="http://testurl.com/now" id="003"> <ns:element2 id="007.0" order="1"> <ns:element3 id="007.1.1"/> </ns:element2> </ns:element1><ns:element1 xmlns:ns="http://testurl.com/now" id="002"> <ns:element2 id="002.1" order="3"> <ns:element3 id="002.1.1"/> </ns:element2> <ns:element2 id="002.2" order="4"> <ns:element3 id="002.1.2"/> </ns:element2> </ns:element1> <element1 id="999"> <element2 id="999.1" order="1"> <element3 id="999.1.1"/> </element2> </element1> </Root> 这当然是可以接受的,如果element1的open和close之间的换行仍在那里会很好 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |