xml使用xsl合并两个文件?

deb*_*ebs 2 xml xslt msxsl

我需要合并两个类似的xml文件,但只包含与常用标记匹配的记录,例如<type>在以下示例中:

file1.xml是

<node>
    <type>a</type>
    <name>joe</name>
</node>
<node>
    <type>b</type>
    <name>sam</name>
</node>
Run Code Online (Sandbox Code Playgroud)

file2.xml是

<node>
    <type>a</type>
    <name>jill</name>
</node>
Run Code Online (Sandbox Code Playgroud)

所以我有一个输出

<node>
    <type>a</type>
    <name>jill</name>
    <name>joe</name>
</node>
<node>
    <type>b</type>
    <name>sam</name>
</node>
Run Code Online (Sandbox Code Playgroud)

在xsl中执行此操作的基础是什么?非常感谢.

小智 5

这个样式表:

<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>
Run Code Online (Sandbox Code Playgroud)

使用此输入(格式良好):

<root>
    <node>
        <type>a</type>
        <name>joe</name>
    </node>
    <node>
        <type>b</type>
        <name>sam</name>
    </node>
</root>
Run Code Online (Sandbox Code Playgroud)

输出:

<root>
    <node>
        <type>a</type>
        <name>jill</name>
        <name>joe</name>
    </node>
    <node>
        <type>b</type>
        <name>sam</name>
    </node>
</root>
Run Code Online (Sandbox Code Playgroud)