xsl-list
[Top] [All Lists]

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

2012-04-02 09:05:50
Hi,
I am working on a stylesheet to process my XML data but can't seem to
resolve duplicates successfully.
The first thing to note is that I am compiling the whole data into a
xsl:variable that I am going to refer at various places in the
stylesheet. But due to this, my preceding-sibling and
following-sibling calls do not work because probably the context is
messed up (in my brain).

I, basically, write the following XML into my xsl variable:-
<LibraryAuthor>
 <LibraryBook 1>  <!-- list of books by this author after removing
duplicates -->
 <LibraryBook 2>
</LibraryAuthor>

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>

Now, here's a snippet of my XSL

<xsl:variable name="authorsList">
  <xsl:call-template name="compileAuthList"/>
</xsl:variable>

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

<xsl:template name="compileAuthList">

  <xsl:for-each select="author">
    <LibraryAuthor>
      <xsl:attribute name="authName">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      ...
      <xsl:call-template name="getBooksOfAuthor">
        <xsl:with-param name="authId" select="@id"/>
      </xsl:call-template>
    </LibraryAuthor>
  </xsl:for-each>
</xsl:template>

<xsl:template name="getBooksOfAuthor">
  <xsl:param name="authId"/>

  <xsl:for-each select="book[@authorId = $authId]">

    <!-- Since the library can contain multiple copies of the same book,
          I need to look for duplicates before I write this LibraryBook

         <xsl:if test="count(preceding-sibling::LibraryBook[@authId =
$authId])">

         doesn't work because we are in the XML doc's <book> node and I
         do not know how to check for previously written <LibraryBook> nodes.
         Let's just say that I cannot run the duplicate check on the
xml data - I have
         to do it here on my <LibraryBook> nodes.
      -->

    <LibraryBook>
      <xsl:attribute name="bookName">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
     <xsl:attribute name="authId">
       <xsl:value-of select="@authorId"/>
     </xsl:attribute>
    </LibraryBook>
  </xsl:for-each>

</xsl:template>

Hoping for some help...

Satish

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--