23

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

<mapping>
    <key value="assigned">
        <a/>
    </key>
    <whatever attribute="x">
        <k/>
        <j/>
    </whatever>
</mapping>

and document2:

<mapping>
    <key value="identity">
        <a/>
        <b/>
    </key>
</mapping>

I want to merge the two like this:

<mapping>
    <key value="identity">
        <a/>
        <b/>
    </key>
    <whatever attribute="x">
        <k/>
        <j/>
    </whatever>
</mapping>

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

EDIT: actually I find I'd rather use an automated tool/script, even writing it by myself, because manually merging some 30 XML files is a bit unwieldy... :-(

2
  • Can you be a little more specific about the rules you want to apply? Will there always only be one element with the same name? Do you go deeper into the tree to compare child elements when both documents have the same element (with identical attributes)? Sep 17, 2008 at 16:56
  • By the way, your example is not well-formed ("whatever" is not closed). Sep 17, 2008 at 16:56

5 Answers 5

8

If you like XSLT, there's a nice merge script I've used before at: Oliver's XSLT page

2
  • 2
    The link is dead.
    – stm
    Nov 3, 2016 at 13:48
  • 3
    Thanks for pointing it out @stm - I've replaced it with the archive.org link to that page Nov 28, 2016 at 10:14
4

I know this is an old thread, but Project: Merge 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.)

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.)

2
  • According to their webpage Project: Merge is the predecessor to Oso XML Merge, and can therefore be considered as a discontinued product at this time. Jan 29, 2019 at 12:49
  • Oso XML Merge has improved functionality over Project: Merge. More information is available at the Oso Corporation website here: osocorporation.com/xmlmerge/index.php (Disclaimer: Oso Corporation is my company, Project: Merge/Oso XML Merge are my products.) :)
    – James
    Mar 5, 2019 at 9:21
4

Check XmlCombiner which is a Java library that implements XML merging in exactly this way. It is loosely based on a similar functionality offered by plexus-utils library.

XmlCombiner default convention is to overwrite the overlapping attributes and elements. But the exact merging behavior can be altered using special 'combine.self' and 'combine.children' attributes.

Usage:

import org.atteo.xmlcombiner.XmlCombiner;

// create combiner
XmlCombiner combiner = new XmlCombiner();
// combine files
combiner.combine(firstFile);
combiner.combine(secondFile);
// store the result
combiner.buildDocument(resultFile);

Disclaimer: I am the author.

2

(also using Oliver's XSLT stlyesheets)

XSLT merge from PowerShell:

param(
[Parameter(Mandatory = $True)][string]$file1,
[Parameter(Mandatory = $True)][string]$file2,
[Parameter(Mandatory = $True)][string]$path
)

# using only abs paths .. just to be safe
$file1 = Join-Path $(Get-Location) $file1
$file2 = Join-Path $(Get-Location) $file2
$path = Join-Path $(Get-Location) $path

# awesome xsl stylesheet from Oliver Becker
# http://web.archive.org/web/20160502194427/http://www2.informatik.hu-berlin.de/~obecker/XSLT/merge/merge.xslt
$xsltfile = Join-Path $(Get-Location) "merge.xslt"

$XsltSettings = New-Object System.Xml.Xsl.XsltSettings
$XsltSettings.EnableDocumentFunction = 1

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xslt.Load($xsltfile , $XsltSettings, $(New-Object System.Xml.XmlUrlResolver))

[System.Xml.Xsl.XsltArgumentList]$al = [System.Xml.Xsl.XsltArgumentList]::new()
$al.AddParam("with", "", $file2)
$al.AddParam("replace", "", "true")

[System.Xml.XmlWriter]$xmlwriter = [System.Xml.XmlWriter]::Create($path)
$xslt.Transform($file1, $al, $xmlwriter)

Using 'plain ol' Saxon:

java -jar saxon9he.jar .\FileA.xml .\merge.xslt with=FileB.xml replace=true
1

Unsure as to whether you want to do this programatically or not.

Edit: Ah, I posted that before the Edit. Don't I look like an idiot now! ;)

If you just want to merge two files together, IBM have an XML Diff and Merge Tool, and there's also Altova's DiffDog.

2
  • Never mind, I've been a bit clumsy and posted the question before the text was complete... who does look like an idiot, now? ;-) As for your answer: thanks a lot, I'm investigating into the tools you suggested and I will let you know if they saved my... bacon :-) Sep 17, 2008 at 7:11
  • 3
    the link to IBM's XML Diff and Merge Tool no longer works
    – chobok
    Apr 3, 2012 at 4:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.