This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: string length


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]