OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   RE: [xml-dev] merging xml files

[ Lists Home | Date Index | Thread Index ]

> 
> 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





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS