xsl-list
[Top] [All Lists]

Re: [xsl] Merging common tags of 2 xml files

2014-08-06 06:08:01
varun bhatnagar varun292006(_at_)gmail(_dot_)com wrote:

*_File1.xml_*

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

<config>
   <version>
      <input00 version ="11"/>
      <name00 name ="abc"/>
   </version>
    <version>
      <input00 version ="22"/>
      <name00 name ="def"/>
   </version>
</config>

*_File2.xml_*

<?xml version="1.0" encoding="UTF-8"?>
<config>
   <version>
      <input00 version ="2"/>
      <name00 name ="xyz"/>
   </version>
     <version>
      <input00 version ="3"/>
      <name00 name ="pqr"/>
   </version>
     <version>
      <input00 version ="4"/>
      <name00 name ="uvw"/>
   </version>
</config>

*_Expected Output.xml_*
*
*
<config>
   <version Label="test" sNo="test">
      <input00 version ="2"/>
      <name00 name ="xyz"/>
   </version>
   <version Label="test" sNo="test">
      <input00 version ="3"/>
      <name00 name ="pqr"/>
   </version>
   <version Label="test" sNo="test">
      <input00 version ="4"/>
      <name00 name ="uvw"/>
   </version>
<version Label="test" sNo="test">
      <input00 version ="11"/>
      <name00 name ="abc"/>
   </version>
   <version Label="test" sNo="test">
      <input00 version ="22"/>
      <name00 name ="def"/>
   </version>
</config>

I think you simply need to write a template for the root that pulls in the nodes from the other document and then your template for "version" simply needs to add the attributes:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:param name="doc2-url" select="'file2.xml'"/>
  <xsl:variable name="doc2" select="document($doc2-url)"/>

  <xsl:output method="xml"  indent="yes"/>

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

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

  <xsl:template match="version">
    <xsl:copy>
      <xsl:attribute name="Level">
        <xsl:value-of select="'test'"></xsl:value-of>
      </xsl:attribute>
      <xsl:attribute name="sNo">
        <xsl:value-of select="'test'"></xsl:value-of>
      </xsl:attribute>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>