[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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

<library>
   <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"/>
   ...
</library>

Then with XSLT 1.0 I would define keys



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<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:variable>

<xsl:template match="/">
  <html>

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

    <body>
      <table>
        <tr>
          <td>Library Name</td>
          ...
        </tr>
      </table>
    </body>
  </html>
</xsl:template>

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


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

</xsl:stylesheet>


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
	http://msmvps.com/blogs/martin_honnen/


Current Thread
Keywords