Re: [xsl] a sorting conundrum

Subject: Re: [xsl] a sorting conundrum
From: JBryant@xxxxxxxxx
Date: Thu, 3 Mar 2005 11:12:30 -0600
If I understand correctly, you want a single list sorted by last name but
wish to retain the first name when it's present.

I wrote a test file:

<names>
  <span class="person">Jenofsky</span>
  <span class="person">Jones</span>
  <span class="person">Zubbard</span>
  <span class="person">Bob Madison</span>
  <span class="person">Oscar Madison</span>
  <span class="person">Felix Unger</span>
</names>

Applied the following XSL file to it:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:template match="/">
    <html>
      <head>
        <title>Sort by Last Name</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="names">
    <ul>
      <xsl:variable name="newnames">
        <xsl:for-each select="span">
          <xsl:choose>
            <xsl:when test="contains(., ' ')">
              <name first="{substring-before(., ' ')}"
last="{substring-after(., ' ')}"/>
            </xsl:when>
            <xsl:otherwise>
              <name last="{.}"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:variable>
      <xsl:for-each select="$newnames/name">
        <xsl:sort select="@last"/>
        <xsl:choose>
          <xsl:when test="@first">
            <li><xsl:value-of select="@first"/><xsl:text>
</xsl:text><xsl:value-of select="@last"/></li>
          </xsl:when>
          <xsl:otherwise>
            <li><xsl:value-of select="@last"/></li>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </ul>
  </xsl:template>

</xsl:stylesheet>

And got the following HTML file:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Sort by Last Name</title>
  </head>
  <body>
    <ul>
      <li>Jenofsky</li>
      <li>Jones</li>
      <li>Bob Madison</li>
      <li>Oscar Madison</li>
      <li>Felix Unger</li>
      <li>Zubbard</li>
    </ul>
  </body>
</html>

Since you say the entries that have first names are already sorted, you
can rely on document order to sort by first name. If they weren't already
sorted, you would probably want to sort by first name as well.

If I have misunderstood what you wanted, please let me know.

HTH

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





John Fitzgibbon <jfitzgibbon@xxxxxxxxxxxxxxxx>
03/03/2005 09:32 AM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
[xsl] a sorting conundrum






Hi,

I am attempting to sort an a list of personal names. All of the names
consist of either a first name followed by a last name or of a last name
only (there are no middle names). Both parts of the name, when present,
are enclosed within the one tag (span) which has a class='person'
attribute, the same tag is used to enclose a last name only. I am
attempting to sort by last name like so

<xsl:for-each select="html/body//span[@class='person']">
<xsl:sort select="substring-after(., ' ')"/>
<xsl:sort select="."/>
<xsl:sort select="substring-before(., ' ')"/>

The problem is that names consisting of a last name only appear first in
my alphabetical sequence and are sorted; these are followed by names
with a first name and a last name and these are also sorted. I require
one alphabetical list rather than two.

Can this be done in one fell swoop, without having to write an XSL style
sheet for the file consisting of two alphabetical sequences?

Any suggestions would be most welcome.

Regards
John

John Fitzgibbon

Galway Public Library
Island House
Cathedral Square
Galway
Ireland

p: 00 353 91 562471
f: 00 353 91 565039
w: http://www.galwaylibrary.ie

*******************************************************************
Ta eolas ata prmobhaideach agus rznda sa rmomhphost seo
agus aon iatan a ghabhann leis agus is leis an duine/na daoine
sin amhain a bhfuil siad seolta chucu a bhaineann siad.
Mura seolam thz, nml tz zdaraithe an rmomhphost ns aon iatan
a ghabhann leis a liamh, a chsipail na a zsaid.
Ma ta an rmomhphost seo faighte agat trm dhearmad,
cuir an seoltsir ar an eolas thrm aischur rmomhphoist
agus scrios ansin i le do thoil.

This e-mail and any attachment contains information which is
private and confidential and is intended for the addressee
only. If you are not an addressee, you are not authorised
to read, copy or use the e-mail or any attachment.
If you have received this e-mail in error, please notify
the sender by return e-mail and then destroy it.
*********************************************************************

Current Thread