[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: merging xml files
I could accomplish my task following the example on http://www.xml.com/pub/a/2003/11/26/q-and-a.html However, the problem I identify is that my xsl is too tied up with the xmls. I find that Michael's flow is better as it checks for 'not' condition instead of constructing everything in the xslt. The problem I am with now is that this step is not very clear on how to implement the 'not' part that is if it is required to add 'X' from 'B' ?? 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> ?? Can you explain Michael? - what should I replace 'E' with? Thanks again. Pank -----Original Message----- From: Michael Kay [mailto:michael.h.kay@n...] Sent: Wednesday, September 01, 2004 7:17 PM To: 'Pankaj Rathi'; xml-dev@l... Subject: RE: merging 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 _____________________________________________________________________ This e-mail has been scanned for viruses by MCI's Internet Managed Scanning Services - powered by MessageLabs. For further information visit http://www.mci.com
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|