Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Merging XML (http://www.velocityreviews.com/forums/t146891-merging-xml.html)

Ravi Shankar 10-12-2005 09:36 PM

Merging XML
 
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



Stefan Schulz 10-13-2005 10:34 AM

Re: Merging XML
 
On Thu, 13 Oct 2005 05:36:34 +0800, Ravi Shankar wrote:

> 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?

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"



Ravi Shankar 10-13-2005 12:38 PM

Re: Merging XML
 
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


"Stefan Schulz" <terra@spacetime.de> wrote in message
news:pan.2005.10.13.10.34.25.320032@spacetime.de.. .
> On Thu, 13 Oct 2005 05:36:34 +0800, Ravi Shankar wrote:
>
>> 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?
>
> --
> You can't run away forever,
> But there's nothing wrong with getting a good head start.
> --- Jim Steinman, "Rock and Roll Dreams Come Through"
>
>




Stefan Schulz 10-13-2005 01:04 PM

Re: Merging XML
 
On Thu, 13 Oct 2005 20:38:31 +0800, Ravi Shankar wrote:

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

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"



Aleksi Kallio 10-13-2005 01:17 PM

Re: Merging XML
 
> 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?


It is actually quite complicated. See:
http://www.cs.wisc.edu/~yuanwang/xdiff.html

And especially:
http://www.cs.wisc.edu/~yuanwang/papers/xdiff.pdf

Ravi Shankar 10-13-2005 07:41 PM

Re: Merging XML
 
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

"Stefan Schulz" <terra@spacetime.de> wrote in message
news:pan.2005.10.13.13.04.51.424114@spacetime.de.. .
> On Thu, 13 Oct 2005 20:38:31 +0800, Ravi Shankar wrote:
>
>> 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. :)
>
> --
> You can't run away forever,
> But there's nothing wrong with getting a good head start.
> --- Jim Steinman, "Rock and Roll Dreams Come Through"
>
>




Stefan Schulz 10-14-2005 08:47 AM

Re: Merging XML
 
On Fri, 14 Oct 2005 03:41:14 +0800, Ravi Shankar wrote:

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

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"



Ravi Shankar 10-14-2005 02:15 PM

Re: Merging XML
 
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
"Stefan Schulz" <terra@spacetime.de> wrote in message
news:pan.2005.10.14.08.47.20.825307@spacetime.de.. .
> On Fri, 14 Oct 2005 03:41:14 +0800, Ravi Shankar wrote:
>
>> 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.
>
> --
> You can't run away forever,
> But there's nothing wrong with getting a good head start.
> --- Jim Steinman, "Rock and Roll Dreams Come Through"
>
>





All times are GMT. The time now is 04:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.