xml 使用 xsl 合并两个文件? [英] xml merge two files using xsl?
本文介绍了xml 使用 xsl 合并两个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要合并两个相似的 xml 文件,但只需要与公共标签匹配的记录,例如
在下面的例子中:
I need to merge two similar xml files, but only records which match on common tags, e.g.<type>
in the following example:
file1.xml 是
file1.xml is
<node>
<type>a</type>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
file2.xml 是
file2.xml is
<node>
<type>a</type>
<name>jill</name>
</node>
所以我有一个输出
<node>
<type>a</type>
<name>jill</name>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
在 xsl 中执行此操作的基础是什么?非常感谢.
What are the basics of doing this, in xsl? Many thanks.
推荐答案
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kElementByType" match="*[not(self::type)]" use="../type"/>
<xsl:param name="pSource2" select="'file2.xml'"/>
<xsl:variable name="vSource2" select="document($pSource2,/)"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="type">
<xsl:variable name="vCurrent" select="."/>
<xsl:call-template name="identity"/>
<xsl:for-each select="$vSource2">
<xsl:apply-templates select="key('kElementByType',$vCurrent)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
有了这个输入(格式正确):
With this input (wellformed):
<root>
<node>
<type>a</type>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
</root>
输出:
<root>
<node>
<type>a</type>
<name>jill</name>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
</root>
这篇关于xml 使用 xsl 合并两个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文