Re: [xsl] Remove duplicates using preceding- or following-siblings

Subject: Re: [xsl] Remove duplicates using preceding- or following-siblings
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Mon, 02 Apr 2012 16:28:58 +0200
Satish wrote:

I am working on a stylesheet to process my XML data but can't seem to
resolve duplicates successfully.

With XSLT 2.0 it sounds as if you could for-each-group, with XSLT 1.0 I would use Muenchian grouping to eliminate duplicates.

Here's a simplified analogy of my XML

   <book id="Bk1" authorId="A1" name="Book1"/>
   <book id="Bk2" authorId="A2" name="Book2"/>
   <author id="A2" name="Auth2" />
   <book id="Bk2" authorId="A2" name="Book2"/>
   <author id="A1" name="Auth1"/>
   <book id="Bk4" authorId="A1" name="Book4"/>

Then with XSLT 1.0 I would define keys

<xsl:stylesheet version="1.0" xmlns:xsl="";>

<xsl:output method="html" indent="yes"/>

<xsl:key name="k1" match="book" use="@authorId"/>
<xsl:key name="k2" match="book" use="concat(@authorId, '|', @id)"/>

<xsl:variable name="authorsList">
  <xsl:apply-templates select="//author" mode="group"/>

<xsl:template match="/">

<xsl:copy-of select="$authorsList"/>

          <td>Library Name</td>

<xsl:template match="author" mode="group">
<LibraryAuthor authName="{@name}">
select="key('k1', @id)[generate-id() =
generate-id(key('k2', concat(current()/@id, '|', @id))[1])]" mode="group"/>

<xsl:template match="book" mode="group">
  <LibraryBook name="{@name}" authId="{@authorId}"/>


The <xsl:copy-of select="$authorsList"/> is just for debugging, you probably want to process that variable further although with XSLT 1.0 you will then need to use exsl:node-set or similar first.


	Martin Honnen --- MVP Data Platform Development

Current Thread