源文件1:
<?xml version="1.0"?>
<items>
<item id='111'>
<aa>111a</aa>
<bb>111b</bb>
</item>
<item id='222'>
<aa>222a</aa>
<bb>222b</bb>
</item>
<item id='333'>
<aa>333a</aa>
<bb>333b</bb>
</item>
</items>
源文件2
<?xml version="1.0"?>
<items>
<item id='222'>
<aa>222aaaaa</aa>
<bb>222bbbbb</bb>
</item>
<item id='444'>
<aa>4444a</aa>
<bb>4444b</bb>
</item>
</items>
检查项目[@id],如果在file1中匹配,则应将整个节点从file2替换为file1,如果item [@id] = 444无法在file1中找到,则追加到file1。
输出xml文件:
<?xml version="1.0"?>
<items>
<item id='111'>
<aa>111a</aa>
<bb>111b</bb>
</item>
<item id='222'>
<aa>222aaaaa</aa>
<bb>222bbbbb</bb>
</item>
<item id='333'>
<aa>333a</aa>
<bb>333b</bb>
</item>
<item id='444'>
<aa>4444a</aa>
<bb>4444b</bb>
</item>
</items>
下面是我的代码:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="aa_file_path">file:///c:/Users/lijinxing/Desktop/test_1.xml</xsl:param>
<xsl:variable name="aa_file" select="document($aa_file_path)"/>
<xsl:template match="/">
<xsl:for-each select="items/item">
<xsl:variable name="id_var" select="@id"/>
<xsl:variable name="content_var">
<xsl:copy-of select="*"/>
</xsl:variable>
<xsl:call-template name="aa">
<xsl:with-param name="id_param" select="$id_var"/>
<xsl:with-param name="content_param" select="$content_var"/>
</xsl:call-template>
</xsl:for-each>
<xsl:for-each select="$aa_file">
<xsl:for-each select="items/item">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="aa">
<xsl:param name="id_param"/>
<xsl:param name="content_param"/>
<xsl:for-each select="$aa_file">
<xsl:for-each select="items/item">
<xsl:variable name="target_id_var" select="@id"/>
<xsl:choose>
<xsl:when test="$target_id_var = $id_param">
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$content_param"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但是下面的变量无法获得整个节点
<xsl:variable name="content_var">
<xsl:copy-of select="*"/>
</xsl:variable>