title: Update an XML file using xslt
body: <p>I have two xml files.</p>
  
  <p>File 1 - </p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;Root&gt;
          &lt;School&gt;
              &lt;section id="12" name="Apple"/&gt;
              &lt;section id="50" name="Newton"/&gt;
          &lt;/School&gt;
          &lt;Students&gt;
              &lt;roll no="111" name="Smith"/&gt;
              &lt;roll no="122" name="Alan"/&gt;
              &lt;roll no="20" name="Bruce"/&gt;
          &lt;/Students&gt;
          &lt;Teachers&gt;
              &lt;Math&gt;
                  &lt;emp id="55" name="Karen"/&gt;
                  &lt;emp id="2" name="David"/&gt;
              &lt;/Math&gt;
              &lt;Science&gt;
                  &lt;emp id="1" name="Thomas"/&gt;
              &lt;/Science&gt;
          &lt;/Teachers&gt;
          &lt;Sports&gt;
              &lt;Indoor&gt;
                  &lt;Boardgame&gt;
                      &lt;game id="12" name="Chess"/&gt;
                  &lt;/Boardgame&gt;
                  &lt;Arcade&gt;
                      &lt;game id="3" name="Car Racing"/&gt;
                  &lt;/Arcade&gt;
              &lt;/Indoor&gt;
              &lt;Outdoor&gt;
                  &lt;Field&gt;
                      &lt;game id="1" name="Football"/&gt;
                      &lt;game id="100" name="Cricket"/&gt;
                  &lt;/Field&gt;
                  &lt;Court&gt;
                      &lt;game id="2" name="Tennis"/&gt;
                  &lt;/Court&gt;
              &lt;/Outdoor&gt;
          &lt;/Sports&gt;
      &lt;/Root&gt;
  </code></pre>
  
  <p>File 2 - </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Updates&gt;
      &lt;School&gt;
          &lt;section id="12" name="Orange"/&gt;
      &lt;/School&gt;
      &lt;Students&gt;
          &lt;roll no="122" name="Sam"/&gt;
      &lt;/Students&gt;
      &lt;Teachers&gt;
          &lt;Math&gt;
              &lt;emp id="300" name="Steve" /&gt;
          &lt;/Math&gt;
      &lt;/Teachers&gt;
      &lt;Sports&gt;
          &lt;Indoor&gt;
              &lt;Boardgame&gt;
                  &lt;game id="37" name="Monopoly"/&gt;
              &lt;/Boardgame&gt;
              &lt;Boardgame2&gt;
                  &lt;game id="36" name="Ludo"/&gt;
              &lt;/Boardgame2&gt;
          &lt;/Indoor&gt;
          &lt;Outdoor&gt;
              &lt;Field&gt;
                  &lt;game id="1" name="Football"/&gt;
                  &lt;game id="100" name="Bull Fighting"/&gt;
              &lt;/Field&gt;
              &lt;Court&gt;
                  &lt;game id="19" name="Badminton"/&gt;
              &lt;/Court&gt;
          &lt;/Outdoor&gt;
          &lt;Computer&gt;
              &lt;game id="10" name="AOE" /&gt;
          &lt;/Computer&gt;
      &lt;/Sports&gt;
  &lt;/Updates&gt;
  </code></pre>
  
  <p>I need to merge the files so that I get the following output. Entries in file2 would overwrite those in file1 if id/no are matching.New elements would be added as required from file2 in the output under the proper hierarchy.</p>
  
  <p>Output of Transformation -</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Root&gt;
      &lt;School&gt;
          &lt;section id="12" name="Orange"/&gt;
          &lt;section id="50" name="Newton"/&gt;
      &lt;/School&gt;
      &lt;Students&gt;
          &lt;roll no="111" name="Smith"/&gt;
          &lt;roll no="122" name="Sam"/&gt;
          &lt;roll no="20" name="Bruce"/&gt;
      &lt;/Students&gt;
      &lt;Teachers&gt;
          &lt;Math&gt;
              &lt;emp id="55" name="Karen"/&gt;
              &lt;emp id="2" name="David"/&gt;
              &lt;emp id="300" name="Steve" /&gt;
          &lt;/Math&gt;
          &lt;Science&gt;
              &lt;emp id="1" name="Thomas"/&gt;
          &lt;/Science&gt;
      &lt;/Teachers&gt;
      &lt;Sports&gt;
          &lt;Indoor&gt;
              &lt;Boardgame&gt;
                  &lt;game id="12" name="Chess"/&gt;
                  &lt;game id="37" name="Monopoly"/&gt;
              &lt;/Boardgame&gt;
              &lt;Arcade&gt;
                  &lt;game id="3" name="Car Racing"/&gt;
              &lt;/Arcade&gt;
              &lt;Boardgame2&gt;
                  &lt;game id="36" name="Ludo"/&gt;
              &lt;/Boardgame2&gt;
          &lt;/Indoor&gt;
          &lt;Outdoor&gt;
              &lt;Field&gt;
                  &lt;game id="1" name="Football"/&gt;
                  &lt;game id="100" name="Bull Fighting"/&gt;
              &lt;/Field&gt;
              &lt;Court&gt;
                  &lt;game id="2" name="Tennis"/&gt;
                  &lt;game id="19" name="Badminton"/&gt;
              &lt;/Court&gt;
          &lt;/Outdoor&gt;
          &lt;Computer&gt;
              &lt;game id="10" name="AOE" /&gt;
          &lt;/Computer&gt;     
      &lt;/Sports&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Below is the XSLT, but it works only for updates, not for inserts.</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:output method="xml" indent="no"/&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;xsl:apply-templates select="node()"&gt;
              &lt;xsl:with-param name="doc-context" select="document('file2.xml')/node()" /&gt;
          &lt;/xsl:apply-templates&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="node()"&gt;
          &lt;xsl:param name="doc-context" /&gt;
  
          &lt;xsl:variable name="id" select="@id" /&gt;
          &lt;xsl:variable name="no" select="@no" /&gt;
  
          &lt;xsl:copy&gt;
              &lt;xsl:copy-of select="@*|$doc-context[@id = $id or @no = $no]/@*" /&gt;
              &lt;xsl:apply-templates select="node()"&gt;
                  &lt;xsl:with-param name="doc-context" select="$doc-context/node()" /&gt;
              &lt;/xsl:apply-templates&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>