Re: [xsl] sorting related issue

Subject: Re: [xsl] sorting related issue
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 14 Dec 2001 16:50:50 +0000
Hi Sanjay,

> I would like to sort my XML on <name> and <type> on the Client side
> and do things like do not display "name" if the next "ErrorMessage"
> record also has the same "name".
>
> I tried checking using "preceding-sibling" but that uses the
> "document order". I am sure this is a trivial issue, but I need some
> clue / pointers / direction.

It sounds as if you want to group your ErrorMessage elements by name.
Unfortunately, grouping isn't 'trivial' in XSLT. You have to pick out
the first ErrorMessages in the document with each name, sort those by
name, and then for each of them display all the ErrorMessages with the
same name, sorted by type.

As an example, have a look at the following:

<!-- index the ErrorMessage elements by their name child -->
<xsl:key name="errors" match="ErrorMessage" use="name" />

<xsl:template match="ErrorMessages">
  <!-- iterate over the first ErrorMessages with each name -->
  <xsl:for-each
      select="ErrorMessage[generate-id() =
                           generate-id(key('errors', name)[1])]">
    <!-- sort them by name -->
    <xsl:sort select="name" />
    <!-- give the name for the group and create a list -->
    <h2><xsl:value-of select="name" /></h2>
    <ul>
      <!-- iterate over all the ErrorMessages with the same name -->
      <xsl:for-each select="key('errors', name)">
        <!-- sort them by type (which is a number) -->
        <xsl:sort select="type" data-type="number" />
        <!-- give some output about the ErrorMessage -->
        <li>
          <xsl:value-of select="type" />:
          <xsl:apply-templates select="details" />
        </li>
      </xsl:for-each>
    </ul>
  </xsl:for-each>
</xsl:template>

For a more detailed description, see
http://www.jenitennison.com/xslt/grouping/muenchian.html.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread