Subject: Merging 2 Xml docs
Posted By: nkuar Post Date: 12/8/2005 2:23:43 AM
Hi all,
  i need to merge two xml documents(can be any two having the same structure). Since I am pretty new to xslt I cannot figure out I can possibly generalise the case for a node containing any number of child nodes. would deeply appreciate any help..

Regards,
Neeraj

Reply By: mhkay Reply Date: 12/8/2005 3:54:35 AM
You need to explain what you mean by merge. If you can't write a specification, try giving a few examples of what you want to achieve. For example, what would be the result of merging the two documents

<a/>

and

<b/>

?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: nkuar Reply Date: 12/8/2005 4:15:32 AM
ya sure, giving two example documents:

1.xml***************************************************************************
<?xml version="1.0" ?>
<catalog>
    <book id="bk101">
        <author>
            <firstname>Gambardella</firstname>
            <secondname>Matthew</secondname>
        </author>
        <title>XML Developer's Guide</title>
    </book>
</catalog>
****************************************************************************
2.xml***********************************************************************
<?xml version="1.0" ?>
<catalog>
        <book id="bk102">
        <author>
            <firstname>Jeanette</firstname>
            <secondname>Dasha</secondname>
        </author>
        <title>Quack the Duck</title>
    </book>
</catalog>
***************************************************************************
result.xml*****************************************************************
<?xml version="1.0" ?>
<catalog>
    <book id="bk101">
        <author>
            <firstname>Gambardella</firstname>
            <secondname>Matthew</secondname>
        </author>
        <title>XML Developer's Guide</title>
    </book>
        <book id="bk102">
        <author>
            <firstname>Jeanette</firstname>
            <secondname>Dasha</secondname>
        </author>
        <title>Quack the Duck</title>
    </book>
</catalog>
***************************************************************
Please note that I want to do this for any 2 xml docs and not the ones presented above. So the tags and number of nodes,child and sibling may vary...



Reply By: mhkay Reply Date: 12/8/2005 4:22:15 AM
OK, you've given one example, where the two documents use the same element names for the outermost element. Now please say what should happen if the two input documents are <a/> and <b/>.

You can solve your given example with:

<xsl:template match="/*">
<xsl:copy>
  <xsl:copy-of select="*"/>
  <xsl:copy-of select="document('two.xml')/*/*"/>
</xsl:copy>
</xsl:template>

But I've no idea whether this solves your general requirement, because you haven't explained the general requirement.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Go to topic 37405

Return to index page 420
Return to index page 419
Return to index page 418
Return to index page 417
Return to index page 416
Return to index page 415
Return to index page 414
Return to index page 413
Return to index page 412
Return to index page 411