<p>I need to "merge" two XML documents, overwriting the overlapsed attributes and elements. For instance if I have <strong>document1</strong>:</p>

<pre><code>&lt;mapping&gt;
    &lt;key value="assigned"&gt;
        &lt;a/&gt;
    &lt;/key&gt;
    &lt;whatever attribute="x"&gt;
        &lt;k/&gt;
        &lt;j/&gt;
    &lt;/whatever&gt;
&lt;/mapping&gt;
</code></pre>

<p>and <strong>document2</strong>:</p>

<pre><code>&lt;mapping&gt;
    &lt;key value="identity"&gt;
        &lt;a/&gt;
        &lt;b/&gt;
    &lt;/key&gt;
&lt;/mapping&gt;
</code></pre>

<p>I want to merge the two like this:</p>

<pre><code>&lt;mapping&gt;
    &lt;key value="identity"&gt;
        &lt;a/&gt;
        &lt;b/&gt;
    &lt;/key&gt;
    &lt;whatever attribute="x"&gt;
        &lt;k/&gt;
        &lt;j/&gt;
    &lt;/whatever&gt;
&lt;/mapping&gt;
</code></pre>

<p>I prefer <strong>Java</strong> or <strong>XSLT</strong>-based solutions, <strong>ant</strong> will do fine, but if there's an easy way to do that in <strong>Rake</strong>, <strong>Ruby</strong> or <strong>Python</strong> please don't be shy :-)</p>

<p><strong>EDIT:</strong> actually I find I'd rather use an automated tool/script, even <a href="http://web.archive.org/web/20100818203850/http://stackoverflow.com:80/questions/58640/great-programming-quotes" rel="nofollow noreferrer">writing it by myself</a>, because manually merging some 30 XML files is a bit unwieldy... :-(</p>

[{'answer_id': 80656, 'author': 'moobaa', 'author_id': 3569, 'author_profile': 'https://Stackoverflow.com/users/3569', 'pm_score': 1, 'selected': False, 'text': '<p>Unsure as to whether you want to do this programatically or not.</p>\n\n<p>Edit: Ah, I posted that before the Edit. Don\'t I look like an idiot now!  ;)</p>\n\n<p>If you just want to merge two files together, IBM have an <a href="http://www.alphaworks.ibm.com/tech/xmldiffmerge" rel="nofollow noreferrer">XML Diff and Merge Tool</a>, and there\'s also Altova\'s <a href="http://www.altova.com/products/diffdog/diff_merge_tool.html" rel="nofollow noreferrer">DiffDog</a>.</p>\n'}, {'answer_id': 174071, 'author': 'Craig.Nicol', 'author_id': 1404, 'author_profile': 'https://Stackoverflow.com/users/1404', 'pm_score': 4, 'selected': True, 'text': '<p>If you like XSLT, there\'s a nice merge script I\'ve used before at:\n<a href="http://web.archive.org/web/20160809092524/http://www2.informatik.hu-berlin.de/~obecker/XSLT/" rel="nofollow noreferrer">Oliver\'s XSLT page</a></p>\n'}, {'answer_id': 3537136, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I know this is an old thread, but <a href="http://www.projectmerge.com" rel="nofollow noreferrer">Project: Merge</a> can do this for you.  It can merge two XML files together, and can be run from the command line, so you can batch everything up together, run it and just resolve any conflicts (such as the changing attribute value of \'key\' in your above example) manually with a few clicks.  (You can tell it to run silently providing there are no conflicts.)</p>\n\n<p>It can perform two-way and three-way comparisons of XML files and two-way and three-way merges.  (Where a three-way operation assumes the two files being compared/merged have a common ancestor.)</p>\n'}, {'answer_id': 27258761, 'author': 'Sławek', 'author_id': 1116153, 'author_profile': 'https://Stackoverflow.com/users/1116153', 'pm_score': 2, 'selected': False, 'text': '<p>Check <a href="https://github.com/atteo/xml-combiner" rel="nofollow noreferrer">XmlCombiner</a> which is a Java library that implements XML merging in exactly this way. It is loosely based on a similar functionality offered by <a href="https://github.com/codehaus-plexus/plexus-utils/blob/master/src/main/java/org/codehaus/plexus/util/xml/Xpp3DomUtils.java" rel="nofollow noreferrer">plexus-utils</a> library.</p>\n\n<p>XmlCombiner default convention is to overwrite the overlapping attributes and elements. But the exact merging behavior can be altered using special <a href="https://github.com/atteo/xml-combiner#controlling-the-merging-behavior" rel="nofollow noreferrer">\'combine.self\' and \'combine.children\'</a> attributes.</p>\n\n<p>Usage:</p>\n\n<pre><code>import org.atteo.xmlcombiner.XmlCombiner;\n\n// create combiner\nXmlCombiner combiner = new XmlCombiner();\n// combine files\ncombiner.combine(firstFile);\ncombiner.combine(secondFile);\n// store the result\ncombiner.buildDocument(resultFile);\n</code></pre>\n\n<p>Disclaimer: I am the author.</p>\n'}, {'answer_id': 43150501, 'author': 'mwallner', 'author_id': 2279385, 'author_profile': 'https://Stackoverflow.com/users/2279385', 'pm_score': 1, 'selected': False, 'text': '<p>(also using <a href="http://web.archive.org/web/20160809092524/http://www2.informatik.hu-berlin.de/~obecker/XSLT/" rel="nofollow noreferrer">Oliver\'s XSLT stlyesheets</a>)</p>\n\n<p>XSLT merge from PowerShell:</p>\n\n<pre><code>param(\n[Parameter(Mandatory = $True)][string]$file1,\n[Parameter(Mandatory = $True)][string]$file2,\n[Parameter(Mandatory = $True)][string]$path\n)\n\n# using only abs paths .. just to be safe\n$file1 = Join-Path $(Get-Location) $file1\n$file2 = Join-Path $(Get-Location) $file2\n$path = Join-Path $(Get-Location) $path\n\n# awesome xsl stylesheet from Oliver Becker\n# http://web.archive.org/web/20160502194427/http://www2.informatik.hu-berlin.de/~obecker/XSLT/merge/merge.xslt\n$xsltfile = Join-Path $(Get-Location) "merge.xslt"\n\n$XsltSettings = New-Object System.Xml.Xsl.XsltSettings\n$XsltSettings.EnableDocumentFunction = 1\n\n$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;\n$xslt.Load($xsltfile , $XsltSettings, $(New-Object System.Xml.XmlUrlResolver))\n\n[System.Xml.Xsl.XsltArgumentList]$al = [System.Xml.Xsl.XsltArgumentList]::new()\n$al.AddParam("with", "", $file2)\n$al.AddParam("replace", "", "true")\n\n[System.Xml.XmlWriter]$xmlwriter = [System.Xml.XmlWriter]::Create($path)\n$xslt.Transform($file1, $al, $xmlwriter)\n</code></pre>\n\n<p>Using \'plain ol\' Saxon:</p>\n\n<pre><code>java -jar saxon9he.jar .\\FileA.xml .\\merge.xslt with=FileB.xml replace=true\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/80609', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4690/']