Search code examples
c++xmlxerces

Xerces: How to merge duplicate nodes?


My question is this:

If I have the following XML:

<root>
  <alpha one="start">
    <in>1</in>
  </alpha>
</root>

and then I'll add the following path:

<root><alpha one="start"><out>2</out></alpha></root>

which results in

<root>
  <alpha one="start">
    <in>1</in>
  </alpha>
</root>
<root>
  <alpha one="start">
    <out>2</out>
  </alpha>
</root>

I want to be able to convert it into this:

<root>
  <alpha one="start">
    <in>1</in>
    <out>2</out>
  </alpha>
</root>

Besides implementing it myself (don't feel like reinventing the wheel today), is there a specific way in Xerces (2.8,C++) to do it?

If so, at which point of the DOMDocuments life is the node merging done? at each insertion? at the writing of the document, explicitly on demand?

Thanks.


Solution

  • If you use xalan its possible to use an xpath to find the element and directly insert into the correc one.

    The following code may be slow but returns all "root" elments with the attribute "one" set to "start".

    selectNodes("//root[@one="start"]")
    

    It is probably better to use the full path

    selectNodes("/abc/def/.../root[@one="start"]")
    

    or if you already have the parent element work relative

    selectNodes("./root[@one="start"]")
    

    I think to get the basic concepts xpath on wikipedia.