Re: [xsl] Categorise Node by Unique Attribute

Subject: Re: [xsl] Categorise Node by Unique Attribute
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 08 Jan 2009 13:15:14 +0100
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:output method="html" indent="yes"/>

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

  <xsl:template match="/">
    <html lang="en">
        <xsl:apply-templates select="authors/author"/>

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

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

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


Read more about that on

	Martin Honnen

Current Thread