Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xml-dev] merging xml files

From: "Michael Kay" <michael.h.kay@--------.--->
To: "'Pankaj Rathi'" <Pankaj.Rathi@------.--->,<xml-dev@-----.---.--->
Date: 9/1/2004 6:17:00 PM
> 
> 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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent