如果元素内容相同,则XSLT合并两个XML文件

时间:2018-06-05 12:52:49

标签: xml xslt

我需要以特定方式合并两个XML文件。

XML-file 1

<csv_data>
<row>
    <important_id>AAAAAAAAAAAAAAAA</important_id>
    <importantstuff>very important</importantstuff>
    <alotmore>stuff</alotmore>
</row>
<row>
    <important_id>BBBBBBBBBBBBBBBB</important_id>
    <importantstuff>very important</importantstuff>
    <alotmore>stuff</alotmore>
</row>

XML-file 2

<csv_data2>
<row1>
    <some_id>213421342134</some_id>
    <important_id>AAAAAAAAAAAAAAAA</important_id>
    <another_id>125135345345</another_id>
    <importantstuffhere>very important stuff here</importantstuffhere>
    <alotmoreandmore>stuff</alotmoreandmore>
</row1>
<row2>
    <some_id>3452345</some_id>
    <important_id>AAAAAAAAAAAAAAAA</important_id>
    <another_id>234234</another_id>
    <importantstuffhere>very important stuff here2</importantstuffhere>
    <alotmoreandmore>stuff2</alotmoreandmore>
</row2>

合并XML文件

<csv_data>
<row>
    <important_id>AAAAAAAAAAAAAAAA</important_id> (from XML-file 1)
    <importantstuff>very important</importantstuff> (from XML-file 1)
    <alotmore>stuff</alotmore> (from XML-file 1)
    <importantstuffhere>very important stuff here</importantstuffhere> (from XML-file 2 row1)
    <importantstuffhere>very important stuff here2</importantstuffhere> (from XML-file 2 row2)
    <importantstuffhere>very important stuff here3</importantstuffhere> (from XML-file 2 row3)
</row>
<row>
    <important_id>BBBBBBBBBBBBBBBB</important_id> (from XML-file 1)
    <importantstuff>very important</importantstuff> (from XML-file 1)
    <alotmore>stuff</alotmore> (from XML-file 1)
    <importantstuffhere>very important stuff here</importantstuffhere> (from XML-file 2 row20)
    <importantstuffhere>very important stuff here2</importantstuffhere> (from XML-file 2 row21)
</row>

XML文件1只有1次,XML文件2可以具有相同的ID几次tousend次。现在我需要XML-file2中的姐妹元素来自与XML-File1合并的相同ID。是不是清楚我的意思?

我是否需要首先列出所有ID?我真的不知道从哪里开始。我只知道我需要[...] apply-templates select =&#34; document(&#39; xml-file&#39;)[...]。

1 个答案:

答案 0 :(得分:0)

我想我找到了一个解决方案:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="row">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:copy-of select="document('file2.xml')//row[important_id = current()/important_id]"/>
    </xsl:copy>
</xsl:template>

输出不是我想要的,但我想我可以在这里工作。

相关问题