[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Remove duplicates using preceding- or following-s

Subject: Re: Remove duplicates using preceding- or following-siblings
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Mon, 02 Apr 2012 16:28:58 +0200
Re:  Remove duplicates using preceding- or  following-s
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.