tags:

views:

1509

answers:

3

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... :-(

A: 

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.

moobaa
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 :-)
Manrico Corazzi
+2  A: 

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

Craig.Nicol
A: 

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

James