<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:param name="StartsWith" />
<xsl:param name="colCount" select="1"/>
<xsl:param name="rowCount" select="10"/>

<xsl:variable name="u" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="l" select="'abcdefghijklmnopqrstuvwxyz'"/>

<xsl:key name="cityName" 
  match="Cities"
  use="translate(substring(CITY,1,1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>

<xsl:template match="/">
 <xsl:apply-templates select="Cities" mode="build-city-table">
  <xsl:with-param name="startLetter" select="$StartsWith"/>
  <xsl:sort select="CITY" />
 </xsl:apply-templates>
</xsl:template>

<xsl:template match="Cities" mode="build-city-table">
 <xsl:param name="max-columns"/>
 <xsl:param name="min-rows-count"/>
 <xsl:param name="startLetter"/>
 
 <xsl:variable name="firstcity">
  <xsl:choose>
   <xsl:when test="string-length($startLetter)=0">
    <xsl:value-of select="translate(substring(CITY[1],1,1),$u,$l)"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$startLetter"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>

 $startLetter: <xsl:value-of select="$startLetter"/>\ 
 $firstcity: <xsl:value-of select="$firstcity"/>\
 key('cityName',$firstcity)[1]: <xsl:value-of select="key('cityName',$firstcity)/CITY[1]"/>\
 key('cityName',$startLetter)[1]: <xsl:value-of select="key('cityName',$startLetter)/CITY[1]"/>
 
</xsl:template>

</xsl:stylesheet>