tags:

views:

331

answers:

3

Pretty simple question, how can I transform a number (1, 2, 3, etc) into a print friendly ordinal number (1st, 2nd, 3rd, etc) using xslt?

Currently the following works for 1-20 but we may be seeing larger sets of entities getting ranked soon:

<xsl:template name="FormatRanking">
 <xsl:param name="Value"></xsl:param>

 <xsl:choose>
  <xsl:when test="$Value = '1'">
   <xsl:value-of select="$Value"/>st
  </xsl:when>
  <xsl:when test="$Value = '2'">
   <xsl:value-of select="$Value"/>nd
  </xsl:when>
  <xsl:when test="$Value = '3'">
   <xsl:value-of select="$Value"/>rd
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="$Value"/>th
  </xsl:otherwise>
 </xsl:choose>

</xsl:template>

The only way I would know how to do this would be to change the xsl:when's:

<xsl:when test="$Value = '1'">
<xsl:when test="$Value = '2'">
<xsl:when test="$Value = '3'">

to (not even sure if this is correct):

<xsl:when test="$Value = '1' or $Value = '21' or $Value = '31' ...">
<xsl:when test="$Value = '2' or $Value = '22' or $Value = '33' ...">
<xsl:when test="$Value = '3' or $Value = '22' or $Value = '33' ...">

I'd like to do something similar to this http://stackoverflow.com/questions/20156/ordinals-in-c# but I'm not sure if it's possible in Xslt.

At this point we only need an English solution.

A: 

Could you use an XSLT extension? I know that EXSLT have a large range of date and time extensions.

Dan Diplo
I am not even sure what an XSLT extension is and I don't have any time right now to look into that, thank you for the suggestion though. I'll make a note to research it later.
Tony Distler
@Dan I've been looking at EXSLT date and time extensions, it doesn't look like they have a solution/extension for this task.
DashaLuna
+5  A: 

I'm not saying it's a good idea to do this in xslt, but...

  <xsl:template name="FormatRanking">
    <xsl:param name="Value"></xsl:param>

    <xsl:choose>
            <xsl:when test="substring($Value,string-length($Value)-1) = '11'">
                    <xsl:value-of select="$Value"/>th
            </xsl:when>
            <xsl:when test="substring($Value,string-length($Value)-1)  = '12'">
                    <xsl:value-of select="$Value"/>th
            </xsl:when>
            <xsl:when test="substring($Value,string-length($Value)-1)  = '13'">
                    <xsl:value-of select="$Value"/>th
            </xsl:when>
            <xsl:when test="substring($Value,string-length($Value)) = '1'">
                    <xsl:value-of select="$Value"/>st
            </xsl:when>
            <xsl:when test="substring($Value,string-length($Value))  = '2'">
                    <xsl:value-of select="$Value"/>nd
            </xsl:when>
            <xsl:when test="substring($Value,string-length($Value))  = '3'">
                    <xsl:value-of select="$Value"/>rd
            </xsl:when>
            <xsl:otherwise>
                    <xsl:value-of select="$Value"/>th
            </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Edit: or a bit neater solution:

  <xsl:template name="FormatRanking">
    <xsl:param name="Value"></xsl:param>

    <xsl:variable name="penultimateChar" select="substring($Value,string-length($Value)-1, 1)"/>
    <xsl:variable name="lastChar" select="substring($Value,string-length($Value))"/>

    <xsl:value-of select="$Value"/>
    <xsl:choose>
            <xsl:when test="$penultimateChar= '1'">
                    <xsl:text>th </xsl:text>
            </xsl:when>
            <xsl:when test="$lastChar = '1'">
                    <xsl:text>st </xsl:text>
             </xsl:when>
            <xsl:when test="$lastChar = '2'">
                    <xsl:text>nd </xsl:text>
            </xsl:when>
            <xsl:when test="$lastChar = '3'">
                    <xsl:text>rd </xsl:text>
            </xsl:when>
            <xsl:otherwise>
                    <xsl:text>th </xsl:text>
            </xsl:otherwise>
    </xsl:choose>

Alohci
+1 for solution #2, this is quite close. I'd refrain from applying string functions to numbers, though, as long as there's a way to do it with number functions.
Tomalak
Yeah. good point.
Alohci
Thank you for the suggestions Alohci, I was originally thinking of doing something like solution #2 but didn't know what string functions to use. However I'm going with Tomalak's solution that mirrors the C# idea and uses math to do it instead of string functions.
Tony Distler
+6  A: 

Here's the solution from "Is there an easy way to create ordinals in C#?", translated to XSLT:

<xsl:template name="FormatRanking">
  <xsl:param name="Value" select="0" />

  <xsl:value-of select="$Value"/>

  <!-- a little parameter sanity check (integer > 0) -->
  <xsl:if test="
    translate($Value, '0123456789', '') = ''
    and
    $Value > 0
  ">
    <xsl:variable name="mod100" select="$Value mod 100" />
    <xsl:variable name="mod10"  select="$Value mod 10" />

    <xsl:choose>
      <xsl:when test="$mod100 = 11 or $mod100 = 12 or $mod100 = 13">
        <xsl:text>th</xsl:text>
      </xsl:when>
      <xsl:when test="$mod10 = 1">
        <xsl:text>st</xsl:text>
      </xsl:when>
      <xsl:when test="$mod10 = 2">
        <xsl:text>nd</xsl:text>
      </xsl:when>
      <xsl:when test="$mod10 = 3">
        <xsl:text>rd</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>th</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>
Tomalak
Thank you Tomalak, this is exactly what I need!
Tony Distler
@Tomalak Thanks a lot!!! This is something I've been looking for. You're a genius :)
DashaLuna