xsl-list
[Top] [All Lists]

Re: [xsl] Checking alphabetical order

2007-09-23 19:12:04



If there isn't, can you please keep the non simple solution (ie the one where you have to create a template to compare two strings) to yourself as I would like to have a stab at it myself.


OK, I did the non-simple solution, so feel free to add your own now.

<xsl:template name="strcmp">
   <xsl:param name="string_1"/>
   <xsl:param name="string_2"/>
<xsl:variable name="alphabet">0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> <xsl:variable name="string_1_first_letter_pos">
     <xsl:call-template name="alphabet_pos">
       <xsl:with-param name="letter" select="substring($string_1, 1, 1)"/>
       <xsl:with-param name="alphabet" select="$alphabet"/>
<xsl:with-param name="alphabet_tot" select="string-length($alphabet)"/> <xsl:with-param name="pos" select="1"/> </xsl:call-template>
   </xsl:variable>
<xsl:variable name="string_2_first_letter_pos">
     <xsl:call-template name="alphabet_pos">
       <xsl:with-param name="letter" select="substring($string_2, 1, 1)"/>
       <xsl:with-param name="alphabet" select="$alphabet"/>
<xsl:with-param name="alphabet_tot" select="string-length($alphabet)"/> <xsl:with-param name="pos" select="1"/> </xsl:call-template>
   </xsl:variable>
<xsl:choose>
     <xsl:when test="$string_1 = $string_2">
       <xsl:text>0</xsl:text>
     </xsl:when>
<xsl:when test="$string_1_first_letter_pos &gt; $string_2_first_letter_pos">
       <xsl:text>-1</xsl:text>
     </xsl:when>
<xsl:when test="$string_1_first_letter_pos &lt; $string_2_first_letter_pos">
       <xsl:text>1</xsl:text>
     </xsl:when>
<xsl:when test="string-length(substring($string_1, 2)) = 0">
       <xsl:text>-1</xsl:text>
     </xsl:when>
<xsl:when test="string-length(substring($string_2, 2)) = 0">
       <xsl:text>1</xsl:text>
     </xsl:when>
<xsl:otherwise>
       <xsl:call-template name="strcmp">
         <xsl:with-param name="string_1" select="substring($string_1, 2)"/>
         <xsl:with-param name="string_2" select="substring($string_2, 2)"/>
       </xsl:call-template>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

 <!-- Capitalises a string -->

 <xsl:template name="capitalise">
   <xsl:param name="string"/>
<xsl:variable name="lowercase_alphabet">
     <xsl:text>abcdefghijklmnopqrstuvwxyz</xsl:text>
   </xsl:variable>
<xsl:variable name="uppercase_alphabet">
     <xsl:text>ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:text>
   </xsl:variable>
<xsl:value-of select="translate($string, $lowercase_alphabet, $uppercase_alphabet)"/>
 </xsl:template>

 <!-- Find the position in a given alphabet of a letter -->

 <xsl:template name="alphabet_pos">
   <xsl:param name="letter"/>
   <xsl:param name="alphabet"/>
   <xsl:param name="alphabet_tot"/>
   <xsl:param name="pos"/>
<xsl:if test="not(contains($alphabet, $letter))">
     <xsl:value-of select="-1"/>
   </xsl:if>

<xsl:if test="substring($alphabet, 1, 1) = $letter or $pos = $alphabet_tot">
     <xsl:value-of select="$pos"/>
   </xsl:if>
<xsl:if test="contains(substring($alphabet, 2), $letter)">
     <xsl:call-template name="alphabet_pos">
       <xsl:with-param name="letter" select="$letter"/>
       <xsl:with-param name="alphabet" select="substring($alphabet, 2)"/>
       <xsl:with-param name="alphabet_tot" select="$alphabet_tot"/>
<xsl:with-param name="pos" select="$pos + 1"/> </xsl:call-template>
   </xsl:if>
 </xsl:template>

If anyone has some hints about how I can do this better, please tell me.


--
Kamal Bhatt


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>