Subject: XML Merge...
Posted By: babloo81 Post Date: 11/14/2003 12:48:49 AM
Hi,

I have 2 XML.
Say I have 2 XMLs,
1: <A><B V='1'/><B V='2'/><B V='3'/></A>
2: <A><B V='4'/><B V='5'/><B V='6'/></A>

I would like to merge them as,
<A><B V='1'/><B V='2'/><B V='3'/><B V='4'/><B V='5'/><B V='6'/></A>

I would like to merge WITHOUT using a loop statement.
Can anyone help me on this.

regds,
Babloo



Reply By: armmarti Reply Date: 11/14/2003 2:13:28 AM
Hi,

this question was answered already: http://p2p.wrox.com/topic.asp?TOPIC_ID=6411

Concerning the looping... Probably not; or you have to write

importNode(child[0]);
importNode(child[1]);
importNode(child[2]);


:)

Regards,
Armen
Reply By: babloo81 Reply Date: 11/14/2003 4:53:08 AM
Hi,
The reply may not suite my problem. Because, the 2 XMLs I had mentioned have m,n childs
So merging the m childs with n childs should be done without looping structure.

Thanks,
Babloo

Reply By: armmarti Reply Date: 11/14/2003 5:44:41 AM
Ok,

you can achieve this with XSLT's xsl:copy-of instruction.
Supposing that your source XML is the first doc you posted and the second is in the file doc2.xml, you can write (not tested):

<xsl:template macth="A">
 <xsl:copy>
  <xsl:copy-of select="B"/>
  <xsl:copy-of select="document('doc2.xml')/A/B"/>
 </xsl:copy>
</xsl:template>


Without XSLT it's probably impossible (in standard W3C DOM), since in
any case you have to get the node-set from one document and add every node in that nodeset to another (so looping can't be avoided). Or you can use dom4j API (using Node.detach() and Branch.addElement() methods to "transfer" a node from one XML doc to another) and you can avoid looping construct(you get the nodeset using XPath expression, which is really a powerful thing ;)) since the transfer process will be done in so-called event-based mode. So you write event-handlers and you'll never have looping constructs.

Regards,
Armen

Go to topic 6482

Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997
Return to index page 996