[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: merging xml files


merge xml files
> 
> Looking at the interest and in reply to Michael, I am expressing my
> requirements again, in case, someone can give a refined solution.
> 
> The task is to merge two xml files. Both the files have the 
> same root. 
> File A might or might not have node X(and its sub elements).
> File B will always have node X(and its sub elements).
> 
> If File A does not contain node X:
> XSLT should get it from file B and insert it in File A under 
> the defined
> location(which is not root).
> 
> If File A contains node X (the location is known):
> XSLT should still get it from file B and replace the one 
> existing in File A.

First let my try to rephrase your requirements using standard terminology,
to check that I have understood them correctly.

The task is to merge two XML documents. The name of the document element of
both documents is the same in both cases [but we don't use this fact].

Document A contains zero or one elements with the name X.
Document B contains exactly one element with the name X.

If document A does not contain an element named X:
the stylesheet should deep-copy the X element from document B and insert it
in Document A at a defined location.

If document A does contain an element named X:
the stylesheet should deep-copy the X element from document B and insert it
in Document A replacing the original X element.

You haven't said what "the defined location" is, but let's suppose that it's
an E element which either does or does not contain X as a child.

So, you start with an identity transform on A:

<xsl:template match="*">
 <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>

If we hit an X, we replace it:

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

If we hit an E that doesn't contain an X, we add the X from B:

<xsl:template match="E[not(child::X)]">
 <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
 <xsl:copy-of select="document('B.xml')//X"/>
</xsl:template>

Michael Kay


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.