使用XSLT合并基于属性值的2个XML文件? [英] Merge 2 XML files based on attribute values using XSLT?
本文介绍了使用XSLT合并基于属性值的2个XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<config>
<state version="10">
<root value="100" group="5">
<leaf number = "2"/>
</root>
<root value="101" group="6">
<leaf number = "3"/>
</root>
</state>
</config>
file2.xml
<config>
<state version="10">
<root value="100" group="5">
<leaf number = "6"/>
</root>
<root value="101" group="6">
<leaf number = "4"/>
</root>
</state>
</config>
output.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>
我尝试根据此处的答案编写XSLT: 如何合并2个XML文件使用XSLT的普通节点?
I tried writing an XSLT based on the answer here: How to merge 2 XML files with common nodes using XSLT?
但是我不知道如何根据属性值进行比较(在这种情况下,属性值和'root'标签的组比较?有人可以帮忙吗?
but I do not know how to compare based on attribute values (in this case, attributes value and group of 'root' tag? Can someone please help?
推荐答案
这应该做到:
<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
示例作为主要输入运行时,将产生:
When run with your file1.xml
example as the main input, this produces:
<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合并基于属性值的2个XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文