Merge 2 XML files with matching 'id' attri

2019-05-23 08:23发布

I want to merge 2 XML files using XSLT when there is matching 'id' attribute.

myFile1.xml (This is the first input file)

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <test>
      <node>
        <type id="a">
          <name>joe</name>
          <name>kill</name>
        </type>
      </node>
      <node>
        <type id="b">
          <name>sam</name>
        </type>
      </node>
    </test>

myFile2.xml (This is the second input file)

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <test>
      <node>
        <type id="a">
        <name>jill</name>
        <name>kill</name>
        </type>
      </node>
    </test>

mergeOutput.xml (This is expected output where id is matched and 2 files are merged)

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <test>
      <node>
        <type id="a">
          <name>joe</name>
         <name>jill</name> 
          <name>Kill</name>
        </type>
      </node>
      <node>
        <type id="b">
          <name>sam</name>
        </type>
      </node>
    </test>

Any XSLT code or link to the code would be helpful. I am not having XSLT knowledge and using this merge to get things working.

标签: xml xslt merge
3条回答
在下西门庆
2楼-- · 2019-05-23 08:54
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
  <xsl:variable name="with" select="'myFile2.xml'" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="//type">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="info" select="document($with)/test/node/type[@id=current()/@id]/." />
     <xsl:for-each select="$info/*">
          <xsl:copy-of select="." />
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

The output of this transform is (See kill is duplicated) -

<?xml version="1.0" encoding="ISO-8859-1"?>
<test>
    <node>
        <type id="a">
            <name>joe</name>
            <name>kill</name>
            <name>jill</name>
            <name>kill</name>
        </type>
    </node>
    <node>
        <type id="b">
            <name>sam</name>
        </type>
    </node>
</test>
查看更多
手持菜刀,她持情操
3楼-- · 2019-05-23 09:16

You can do this in LINQ2XML

XElement doc1=XElement.Load("myFile1.xml");
XElement doc2=XElement.Load("myFile2.xml");
XElement doc3=XElement.Load("myFile1.xml");//will contain mergeOutput

var dec=doc3.Descendants().Elements("type").ToList();
int i=0;
foreach(XElement elm1 in doc1.Descendants().Elements("type"))
foreach(XElement elm2 in doc2.Descendants().Elements("type"))
if(elm1.Attribute("id").Value==elm2.Attribute("id").Value)
dec[i++].Add(elm2.Elements());

doc3;//your mergeOutput
查看更多
做个烂人
4楼-- · 2019-05-23 09:18

XSLT 2.0 solution:

<xsl:template name="main">
  <test>
    <node>
      <xsl:for-each-group select="(doc('myFile1.xml'), doc('myFile2.xml'))/test/node/type"
                          group-by="@id">
        <type id="{@id}">
          <xsl:copy-of select="current-group()/*"/>
        </type>
      </xsl:for-each-group>
    </node>
  </test>
</xsl:template>
查看更多
登录 后发表回答