xsl-list
[Top] [All Lists]

Re: [xsl] Categorise Node by Unique Attribute

2009-01-08 07:15:43
William Warby wrote:

I want to group nodes in an XML file, placing a heading above each category. The category is defined in an attribute which may be the same for a series of records. The code sample below shows what I want to do. I've been going round in circles trying combinations of variables, templates, for-each loops and I just can't seem to get a handle on the problem. Any help would be very gratefully received.

Here is an XSLT 1.0 stylesheet that uses Muenchian grouping:

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

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

  <xsl:key name="cat"
           match="book"
           use="concat(parent::author/@name, '|', @genre)"/>

  <xsl:template match="/">
    <html lang="en">
      <head>
        <title>Books</title>
      </head>
      <body>
        <h1>Books</h1>
        <xsl:apply-templates select="authors/author"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="author">
    <h2>
      <xsl:value-of select="@name"/>
    </h2>
<xsl:apply-templates select="book[generate-id() = generate-id(key('cat', concat(parent::author/@name, '|', @genre))[1])]" mode="group"/>
  </xsl:template>

  <xsl:template match="book" mode="group">
    <h3>
      <xsl:value-of select="@genre"/>
    </h3>
    <ul>
<xsl:apply-templates select="key('cat', concat(parent::author/@name, '|', @genre))"/>
    </ul>
  </xsl:template>

  <xsl:template match="book">
    <li>
      <xsl:value-of select="@name"/>
    </li>
  </xsl:template>

</xsl:stylesheet>

Read more about that on http://www.jenitennison.com/xslt/grouping/index.xml
--

        Martin Honnen
        http://JavaScript.FAQTs.com/

--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>