mege two separate xml nodes into one 2007-05-25 - By Matthijs Wensveen
Looking at the expected output, there seems to be some logic to implement. The place to do that is where Mukul put his copy-of-statement, or maybe even the Book element itself. My experience with 'merging' is that you really have to know what you're doing. What do you do when some attributes match, but others don't? What if the element text or children don't match? Usually this depends on business logic, and I'm not a big fan of implementing that in xslt. I'd put it somewhere where it can be unit tested (there's bound to be some unit tester for xslt, I guess... anyone?).
Also, be careful when your boss says: "oh you just have to merge the data", because the word 'merge' sounds as if there is no logic, but almost always there is. I've been there :)
Matthijs
Mukul Gandhi wrote: > Following is a solution for this: > > <?xml version="1.0" encoding="UTF-8" ?> > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0"> > > <xsl:output method="xml" indent="yes" /> > > <xsl:template match="AllBooks"> > <AllBooks> > <BookList> > <xsl:for-each select="*/*"> > <Book id="{position()}"> > <xsl:copy-of select="@*[not(name() = 'id')]" /> > </Book> > </xsl:for-each> > </BookList> > </AllBooks> > </xsl:template> > > </xsl:stylesheet> > > On 5/24/07, Alan Andrade <alan.andrade@(protected)> wrote: >> >> >> >> How do I mege the 2 nodes? >> >> INPUT: >> >> >> >> <AllBooks> >> >> <BookList1> >> >> <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/> >> >> <Book id="3" upc= "3" someotherID= "booklist1" salePrice= "3"/> >> >> <Book id="4" upc= "4" someotherID= "booklist1" salePrice= "4"/> >> >> </BookList1> >> >> <BookList2> >> >> <Book id="2" upc= "2" someotherID= "booklist2" salePrice= "2"/> >> >> <Book id= "3" upc= "3" someotherID= "booklist2" salePrice= "3"/> >> >> <Book id= "4" upc= "4" someotherID= "booklist2" salePrice= "4"/> >> >> <Book id= "5" upc= "5" someotherID= "booklist2" salePrice= "5"/> >> >> </BookList2> >> >> </AllBooks> >> >> >> >> OUTPUT: >> >> >> >> <AllBooks> >> >> <BookList> >> >> <Book id="1" upc= "1" someotherID= "booklist1" salePrice= "1"/> >> >> <Book id="2" upc= "3" someotherID= "booklist1" salePrice= "3"/> >> >> <Book id="3" upc= "4" someotherID= "booklist1" salePrice= "4"/> >> >> <Book id="4" upc= "2" someotherID= "booklist2" salePrice= "2"/> >> >> <Book id= "5" upc= "3" someotherID= "booklist2" salePrice= "3"/> >> >> <Book id= "6" upc= "4" someotherID= "booklist2" salePrice= "4"/> >> >> <Book id= "7" upc= "5" someotherID= "booklist2" salePrice= "5"/> >> >> </BookList> >> >> </AllBooks> >> >> >> >> Thanks >> >> Alan >> >> >> >> >> >> >> >> >> ________________________________ >> >> >> >> >> The information contained in this e-mail message is intended only for >> the >> personal and confidential use of the recipient(s) named above. This >> message >> may be an attorney-client communication and/or work product and as >> such is >> privileged and confidential. If the reader of this message is not the >> intended recipient or an agent responsible for delivering it to the >> intended >> recipient, you are hereby notified that you have received this >> document in >> error and that any review, dissemination, distribution, or copying of >> this >> message is strictly prohibited. If you have received this >> communication in >> error, please notify us immediately by e-mail, and delete the original >> message. > >
-- Matthijs Wensveen Func. Internet Integration W http://www.func.nl T +31 20 4230000 F +31 20 4223500
|
|