Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Alphabetic sort retaining first letters as Headers

From: Jeni Tennison <jeni@---------------->
To:
Date: 11/1/2004 10:13:00 PM
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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent