<config>
<state version="10">
<root value="100" group="5">
<leaf number = "2"/>
</root>
<root value="101" group="6">
<leaf number = "3"/>
</root>
</state>
</config>
<config>
<state version="10">
<root value="100" group="5">
<leaf number = "6"/>
</root>
<root value="101" group="6">
<leaf number = "4"/>
</root>
</state>
</config>
<config>
<state version="10">
<root value="100" group="5">
<leaf number = "2"/>
<leaf number = "6"/>
</root>
<root value="101" group="6">
<leaf number = "3"/>
<leaf number = "4"/>
</root>
</state>
</config>
我尝试根据这里的答案编写一个XSLT: How to merge 2 XML files with common nodes using XSLT?
但我不知道如何根据属性值进行比较(在这种情况下,属性值和'root'标签组?有人可以帮忙吗?
答案 0 :(得分:1)
这应该这样做:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:apply-templates
select="document('file2.xml')
/config/state[@version = current()/../@version]
/root[@value = current()/@value and
@group = current()/@group]/*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
以file1.xml
示例作为主输入运行时,会产生:
<config>
<state version="10">
<root value="100" group="5">
<leaf number="2" />
<leaf number="6" />
</root>
<root value="101" group="6">
<leaf number="3" />
<leaf number="4" />
</root>
</state>
</config>