mnvbrtn
mnvbrtn

Reputation: 568

Compare two xmls and get the common nodes

I have two xml's . Need to read these two and create new xml with only common elements

First XML:

<Items>
    <array>
        <item name="A">
            <name>A</name>
            <indate>20141112</indate>
            <inno>2</inno>
            <status>1</status>
            <level>12</level>
            <size>.1</size>
            <text>item a text </text>
        </item>
        <item name="B">
            <name>B</name>
            <indate>20141012</indate>
            <inno>5</inno>
            <status>1</status>
            <level>13</level>
            <size>.5</size>
            <text>item b text </text>
        </item>
        <item name="C">
            <name>C</name>
            <indate>20140912</indate>
            <inno>6</inno>
            <status>1</status>
            <level>12</level>
            <size>.2</size>
            <text>item c text </text>
        </item>
       </array>
</Items>

Second XML

<Items>
    <array>
     <item name="A">
            <name>A</name>
            <ondate>20140612</ondate>
            <onno>9</onno>
            <status>1</status>
            <level>12</level>
        </item>
        <item name="B">
            <name>B</name>
            <ondate>20140212</ondate>
            <inno>7</inno>
            <status>1</status>
            <level>13</level>
            <size>.5</size>
        </item>
        <item name="D">
            <name>D</name>
            <indate>20140712</indate>
            <inno>9</inno>
            <status>1</status>
            <level>12</level>
        </item>
       </array>
</Items>

Output should be

<Items>
    <array>
     <item name="A">
            <name>A</name>
            <status>1</status>
            <level>12</level>
        </item>
        <item name="B">
            <name>B</name>
            <status>1</status>
            <level>13</level>
            <size>.5</size>
        </item>
       </array>
</Items>

How to do this with xslt 1.0. Out put should have the common elements from both the xml's .

Upvotes: 2

Views: 749

Answers (1)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56172

Use this XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:data="data">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="xml2" select="document('xml2.xml')//item" />

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

  <xsl:template match="item">
    <xsl:variable name="item" select="$xml2[@name = current()/@name]"/>
    <xsl:if test="$item">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>

        <xsl:for-each select="*">
          <xsl:if test="$xml2/*[name() = name(current()) and . = current()/.]">
            <xsl:apply-templates select="."/>
          </xsl:if>
        </xsl:for-each>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

When applied to the first XML and xml2.xml is the second XML (from your question) produces:

<Items>
  <array>
    <item name="A">
      <name>A</name>
      <status>1</status>
      <level>12</level>
    </item>
    <item name="B">
      <name>B</name>
      <status>1</status>
      <level>13</level>
      <size>.5</size>
    </item>
  </array>
</Items>

Upvotes: 2

Related Questions