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

Re: Alphabetic sort retaining first letters as Headers

Subject: Re: Alphabetic sort retaining first letters as Headers
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 1 Nov 2004 20:59:52 +0000
xsl sort grouping alphabetic
Hi Dave,

> I have been trying to sort and display a a set of elements
> alphabetically and grab the first letter for a group header. The
> header would only appear once and won't appear if there is an empty
> set (eg. 'X').

This is a grouping problem. Using the Muenchian method for grouping,
you should do this:

First, define a key that indexes each of the elements using the first
letter of the element:

<xsl:key name="elements" match="element" use="substring(., 1, 1)" />

This means you can get all the elements that begin with 'B', using:

  key('elements', 'B')

You can then access the first element of each group using:

  element[generate-id(.) =
          generate-id(key('elements', substring(., 1, 1))[1])]

If I understand what you're after correctly, the code for your actual
problem should look something like:

<xsl:key name="maps" match="maps | maptitle"
         use="substring(., 1, 1)" />

<xsl:template match="body" mode="alpha-title">
  <xsl:for-each
    select="(part/chapters/mapunits/maps |
             part/chapters/mapunits/maptitle)
              [generate-id(.) =
               generate-id(key('maps', substring(., 1, 1))[1])]">
    <xsl:sort select="." />
    <xsl:variable name="firstletter" select="substring(., 1, 1)" />
    <div class="trackNumber">
      <xsl:value-of select="$firstletter" />
    </div>
    <xsl:for-each select="key('maps', $firstletter)">
      <div class="mapTitle">
        <a href="../../Maps/{../../@chap}/{../@mapunitNumber}/{@mapNumber}/Normal/">
          <xsl:value-of select="." />
        </a>
      </div>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

Cheers,

Jeni

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

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.