[xsl] Alphabetic sort retaining first letters as Headers

Subject: [xsl] Alphabetic sort retaining first letters as Headers
From: David Laurie <dlaurie@xxxxxxxxxxx>
Date: Mon, 1 Nov 2004 11:48:48 -0700
Hello XSL list,

I tried to send this last week but somehow my webmail converted all of my html 
and xml code snippet into web display text. Apparently I need help with my 
help :).

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'). For example I want to 
transform something like:

<content>
<element>Bob</element>
<element>Craig</element>
<element>Al</element>
<element>Frank</element>
<element>Bill</element>
<element>Barb</element>
<element>Andrew</element>
<element>Felix</element>
<element>Farah</element>
</content>

into:

A
Andrew
Al

B
Barb
Bill
Bob

C
Craig

F
Farah
Felix
Frank

The xml I am using is not flat, ie. there are elements being sorted that are 
'cousins' of one another, not just siblings. The xsl I have written so far 
gets the sort OK and strips off the first letter but does so for each element 
sorted.

I have tried to test the pulled letter against that of the following-sibling 
but the sort interferes with that. I have also tried to create a variable for 
the letter at the next position() but that didn't work either.

The xsl I use:

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


A relevant snippet of xml schema:

<body>
       <part>
            <chapters>
                 <mapunits>
                      <maptitle/>
                      <maps/>
                 </mapunits>
            </chapters>
        </part>
</body>

I am pulling all the maptitles and maps from all of the parts.

Has anyone tried something like this before?

Thanks for your time,

Dave

David Laurie
MA Humanities Computing

Current Thread