我想将 2 个具有相同结构的 XML 文件合并为一个。例如;
测试1.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>
and 测试2.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>
创造
测试输出.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 上发现了一个有用的问题,并提出了这个问题;
合并.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 来参与其中)
xmllint -xinclude Merge.xml
但这不起作用,它抱怨各种事情,这些事情似乎与 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>
如果我省略 xpointer 属性合并.xml然后我得到一些合理的输出,但它所做的不仅仅是包含我想要的元素。
有人可以就我在 xpointer 上做错的事情提供一些建议吗?
感谢期待。