Merging XML

R

Ravi Shankar

HI all,

I would like to merge two or more XML documents. But we need to make sure
that merging is not appending. All common tags should be at one place etc
etc. ANyone has written something similar? Any open source for this?

Thanks and regards,
Ravi
 
S

Stefan Schulz

HI all,

I would like to merge two or more XML documents. But we need to make sure
that merging is not appending. All common tags should be at one place etc
etc. ANyone has written something similar? Any open source for this?

What do you mean, merge two XML documents? Suppose you have

<root>
<achild anattribute="foo" anotherattribute="bar"/>
<achild>
<agrandchild nothing="happens"/>
</achild>
</root>

and

<ca:calculation xmlns:ca="http://xxxxxxxxx.invalid/calculate">
<ca:add>
<ca:constant value="7.9"/>
<ca:sub>
<ca:constant value="8.1"/>
<ca:constant value="3.8"/>
</ca:sub>
</ca:add>
</ca:calculation>

What would the merged document look like?
 
R

Ravi Shankar

Dear Stefan,

In the examples you had used, it will be just appending.

To make it clear, let me illustrate with another.
1) Input1.xml

<?xml version="1.0"?>
<app name="testapp" lifetime="900">
<mainmenu>
<menu id="1" caption="test"/>
<menu id="2" caption="another test"/>
</mainmenu>
<forms>
<testform autosize="1"/>
<testform2 autosize="0"/>
</forms>
</app>

2) Input2.xml

<?xml version="1.0" encoding="UTF-8"?>
<app lifetime="100">
<mainmenu>
<menu id="2" caption="my test"/>
<menu id="3" caption="my menu"/>
</mainmenu>
<forms>
<testform2 autosize="1"/>
</forms>
</app>

3) Result.xml should be

<?xml version="1.0" encoding="UTF-8"?>
<app name="testapp" lifetime="100">
<mainmenu>
<menu id="1" caption="test"/>
<menu id="2" caption="my test"/>
<menu id="3" caption="my menu"/>
</mainmenu>
<forms>
<testform autosize="1"/>
<testform2 autosize="1"/>
</forms>
</app>


WHat I request is an answer, not specific to above example, but generic. I
mean, without hardcoding any tag names, I would like to generate it
dynamically. Also, it is possible that number of input XMLs to be merged may
not be two.

Hence to put it briefly, generate an XML file, by merging n different XML
files, where 2<= n <= 25

Please help, thanks

Warmest regards,
Ravi
 
S

Stefan Schulz

Dear Stefan,

In the examples you had used, it will be just appending.

You still need to specify a number of things:

* What happens when the root elements mismatch?
* What happens if there are common subtrees A and B, but one
of the documents has a node C between these nodes?

Once you specify this, i can try coming up with an idea how to solve the
problem. I will not give you a complete implementation, however. :)
 
R

Ravi Shankar

Dear Stefan,

Thanks for reply.

My answers:-

1) Assume roots will never be different
2) If one of the document has a node C, then that will be copied as it is to
destination

Best regards,
Ravi
 
S

Stefan Schulz

1) Assume roots will never be different
2) If one of the document has a node C, then that will be copied as it is to
destination

Hmm, this is a problem that requires a significant amount of work. Look
into text diff algorithms. If you process nodes instead of lines, you
should be able to handle a single node much like a document. Recurse for
matching nodes.
 
R

Ravi Shankar

Dear all,

The problem had been closed. With some diifculty, I could write a 240 line
XSL stylesheet which accomplished the task beautifully. Now it is in our
production and is live :)

I have written it such that if we set a parameter "replace", then it will
function as my requirement. If you give parameter "merge", it will simply
merge the sources. If instead you give "with" followed by a node name, then
that won't include that node in operation at all. Kudos to XSLT.

Best regards,
Ravi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top