[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] string length


Subject: Re: [xsl] string length
From: Mike Brown <mike@xxxxxxxx>
Date: Wed, 15 Aug 2001 18:22:43 -0600 (MDT)

Eric Schenfeld wrote:
> I would like to access the last letter of a string.

The XPath spec contains a String Functions section that you will find 
helpful. http://www.w3.org/TR/xpath#section-String-Functions

Yes, there is a string-length function.  

> The purpose is to format numbers as 21st, 35th, 87th and so on.

Something like this (untested code):

<xsl:variable name="num" select="'21'"/> <!-- for example -->
<xsl:variable name="lastChar" select="substring($num,string-length($num))"/>
<xsl:variable name="formattedNum">
  <xsl:value-of select="$num"/>
  <xsl:choose>
    <xsl:when test="$lastChar='1'">st</xsl:when>
    <xsl:when test="$lastChar='2'">nd</xsl:when>
    <xsl:when test="$lastChar='3'">rd</xsl:when>
    <xsl:when test="not(translate($lastChar,'4567890',''))">th</xsl:when>
  </xsl:choose>
</xsl:variable>
<xsl:value-of select="concat('unformatted: ',$num,'&#10;formatted: ',$formattedNum)"/>

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords