Re: [xml] Merging multiple XML trees

"Dustin D. Trammell" <dtrammell tippingpoint com> writes:


First let me say that I am somewhat new to XML, and very new to libxml2.
I tried searching this list's archives for previous discussion on what I
am trying to do and also perused the reference section of functions on
the reference area for functions that looked appropriate but
so far have not found what I am looking for.  Also, please advise if
this is not the appropriate place for this type of development question.
Any pointers in the right direction would be most appreciated.

What I am trying to do is have my application read in two XML trees.
I'm doing this with the get_xml_doc_ptr() function.  The two XML trees
have some common elements and some uncommon elements between them.  Many
of the common elements have some common attributes and some uncommon
attributes between the matching elements.  I want to merge these two
trees into either a third, new tree or update one of the existing trees
so that colliding elements, attributes, and values are preserved from
one of the trees and all uncommon elements between the two trees will be
available in the resultant tree.  For example:

Tree A:
<myelement attribute="yes!">
      <subelement3/ threeattribute1="yes!"/>

Tree B:
<myelement attribute="no!">
      <subelement1 oneattribute="yes!"/>
      <subelement2 twoattribute="yes!"/>
      <subelement3 threeattribute2="yes!"/>

Merging the two, and preserving values from Tree A, the resultant tree
would look like the following.  Note the preservation of Tree A's
colliding "attribute" attribute of the "myelement" element and the
aggregation of all the subelements and subelement attributes in the
subelement section:

<myelement attribute="yes!">
      <sublement1 oneattribute="yes!"/>
      <subelement2 twoattribute="yes!"/>
      <subelement3 threeattribute1="yes!" threeattribute2="yes!"/>

Does libxml2 provide any functions that already do this or some similar
tree merger?  It would actually be more beneficial to merge elements
individually, not the entire tree.  For example, consider the
"myelement" elements above to not be the root nodes of the tree and both
be somewhere deep within two other mostly unrelated XML documents.
Seeking to both of them with something like XPath and then creating a
new XML document out of just those element sections is really what I'm
trying to accomplish.

So far, from looking at the reference area's function lists
it appears I may have to code up this functionality manually, building a
third tree from scratch (not hard, I already found the tutorial on that
one...) and then stepping through the two (or more) source trees element
by element, attribute by attribute and cross checking them all manually
before deciding what to add to the resultant third tree.  That seems
fairly messy, and I'd rather not do that if the capability is already
within libxml2 to do the bulk of the work itself.

Thanks in advance for any advice or pointers in the right direction any
of you may be able to provide...

I'd use xslt for this. It would be a lot easier to express.

Nic Ferrier

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]