This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Merging Two Files and Removing Duplicate Nodes
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] Merging Two Files and Removing Duplicate Nodes
- From: "Michael Kay" <mhkay at iclway dot co dot uk>
- Date: Fri, 6 Jul 2001 19:06:53 +0100
- Reply-To: xsl-list at lists dot mulberrytech dot com
> 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'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>
Unless your processor is very clever, that's going to have O(m*n)
performance. I'd strongly recommend using keys, even though you can no
longer do the selection in one XPath statement:
<xsl:key name="prod2" match="second-file-list/product" use="code"/>
<xsl:template match="/">
<xsl:for-each select="first-file-list/product">
<xsl:variable name="code" select="code"/>
<xsl:for-each select="document($second-file)">
<xsl:apply-templates select="key('prod2', $code)">
This will process all the products on the second file that also exist on the
first file; products that are not present on both files will not be
processed.
Mike Kay
Software AG
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list