Re: [xsl] Using XSLT to merge two XML Nodes

Subject: Re: [xsl] Using XSLT to merge two XML Nodes
From: omprakash.v@xxxxxxxxxxxxx
Date: Fri, 19 Aug 2005 23:24:31 +0530
Hi,
        Please use the below stylesheet to achieve what you want.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:n1="urn:n1.nmspace">

<xsl:output method="xml"></xsl:output>

<xsl:key name="book1" match="BookList1/Book" use="@id"/>
<xsl:key name="book2" match="BookList2/Book" use="@id"/>


             <xsl:template match="/">

             <xsl:variable name="allbooks" select="/AllBooks/*"/>

            <xsl:apply-templates select="$allbooks/Book">
            <xsl:sort select="@id"/>
            </xsl:apply-templates>

             </xsl:template>

      <xsl:template match="Book">

      <xsl:call-template name="book1"/>

       </xsl:template>

 <xsl:template name="book1">

  <xsl:variable name="books_1" select="key('book1', @id)"/>
  <xsl:variable name="books_2" select="key('book2', @id)"/>

 <xsl:if test="$books_1">
      <xsl:copy-of select="key('book1', @id)"/>
</xsl:if>
<xsl:if test="not(key('book1', @id))">
<xsl:text>Not Found
</xsl:text>
</xsl:if>
<xsl:if test="not(key('book2', @id))">
<xsl:text>Not Found
</xsl:text>
</xsl:if>
  <xsl:if test="$books_2 and not($books_1)">
      <xsl:copy-of select="key('book2', @id)"/>
</xsl:if>

 </xsl:template>


</xsl:stylesheet>

cheers,
prakash





                                                                                                                                        
                      Santosh Shanbhag                                                                                                  
                      <santoshlovesjava         To:      xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                                
                      @gmail.com>               cc:      (bcc: omprakash.v/Polaris)                                                     
                                                Subject: [xsl] Using XSLT to merge two XML Nodes                                        
                      08/19/2005 09:34                                                                                                  
                      PM                                                                                                                
                      Please respond to                                                                                                 
                      xsl-list                                                                                                          
                                                                                                                                        
                                                                                                                                        




Hi,

I have a document with two nodes that I have to merge:

<AllBooks>
 <BookList1>
   <Book id=3D"1" upc=3D"1" someotherID=3D"booklist1" salePrice=3D"1"/>
   <Book id=3D"3" upc=3D"3" someotherID=3D"booklist1" salePrice=3D"3"/>
   <Book id=3D"4" upc=3D"4" someotherID=3D"booklist1" salePrice=3D"4"/>
 </BookList1>
 <BookList2>
   <Book id=3D"2" upc=3D"2" someotherID=3D"booklist2" salePrice=3D"2"/>
   <Book id=3D"3" upc=3D"3" someotherID=3D"booklist2" salePrice=3D"3"/>
   <Book id=3D"4" upc=3D"4" someotherID=3D"booklist2" salePrice=3D"4"/>
   <Book id=3D"5" upc=3D"5" someotherID=3D"booklist2" salePrice=3D"5"/>
 </BookList2>
</AllBooks>

My output needs to be:

<AllBooks>
 <BookList>
   <Book id=3D"1" upc=3D"1" someotherID=3D"booklist1" salePrice=3D"1"/>
   <NotFound/>
   <NotFound/>
   <Book id=3D"2" upc=3D"2" someotherID=3D"booklist2" salePrice=3D"2"/>
   <Book id=3D"3" upc=3D"3" someotherID=3D"booklist1" salePrice=3D"3"/>
   <Book id=3D"3" upc=3D"3" someotherID=3D"booklist2" salePrice=3D"3"/>
   <Book id=3D"4" upc=3D"4" someotherID=3D"booklist1" salePrice=3D"4"/>
   <Book id=3D"4" upc=3D"4" someotherID=3D"booklist2" salePrice=3D"4"/>
   <NotFound/>
   <Book id=3D"5" upc=3D"5" someotherID=3D"booklist2" salePrice=3D"5"/>





This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

Current Thread