[xsl] Merging Two Files and Removing Duplicate Nodes

Subject: [xsl] Merging Two Files and Removing Duplicate Nodes
From: Dendler James A <dendler.ja@xxxxxxxxxxx>
Date: Fri, 6 Jul 2001 13:03:43 -0400
I am trying to merge two files so that the output contains only products
that have matching codes on both files.  In addition, I don't want the
output to contain duplicate child elements under product (i.e. if both have
a name element, I only want to output the name element from the first file).

Input Files:

<first-file-list>
     <product>
          <code>0002</code>  <name>New Name</name>  <class>33</class>
     </product>
     ...
</first-file-list>

<second-file-list>
     <product>
          <code>0001</code>   <name>Old Name</name>   <price>2.99</price>
     </product>
     <product>
          <code>0002</code>   <name>Old Name</name>   <price>2.99</price>
     </product>
     ...
</second-file-list>

Desired output:

<output-list>
     <product>
          <code>0002</code>  <name>New Name</name>  <class>33</class>
<price>2.99</price>
     </product>
     ...
</output-list>

I've been able to get matching products using the folllowing xsl and then
copy the children from each file.  I just haven't figured how to exclude
child nodes from the second file if they exist on the first file.

<xsl:template match="/">
    <xsl:apply-templates
select="*//product[./code=document($second-file)//product/code]"/>
</xsl:template>

<xsl:template match="product">
  <xsl:copy>
    <xsl:call-template name="obtain-view-info">
      <!-- This template copies the elements from the second file -->
      <xsl:with-param name="product-code" select="code"/>  
    </xsl:call-template>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="copy"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()" mode="copy">
  <xsl:copy>
    <xsl:apply-templates select="@*" mode="copy"/>
    <xsl:apply-templates mode="copy"/>
  </xsl:copy>
</xsl:template>

<xsl:template name="obtain-view-info">
  <xsl:param name="product-code"/>
  <!-- Obtain the matching product information from the view file -->
  <xsl:apply-templates
select="document($second-file)//product[code=$product-code]"
mode="import"/>
</xsl:template>

<xsl:template match="product" mode="import">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="import"/>
</xsl:template>

<xsl:template match="@*|node()" mode="import">
  <xsl:copy>
    <xsl:apply-templates select="@*" mode="import"/>
    <xsl:apply-templates mode="import"/>
  </xsl:copy>
</xsl:template>

Thanks for any help you can give me.

Jim Dendler
*****************************************************************
DISCLAIMER:   The information contained in this e-mail may be confidential
and is intended solely for the use of the named addressee.  Access, copying
or re-use of the e-mail or any information contained therein by any other
person is not authorized.  If you are not the intended recipient please
notify us immediately by returning the e-mail to the originator.    

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread