Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform a number (1,2,3, etc) into an ordinal number (1st, 2nd, 3rd, etc) using xslt

Tags:

xslt

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 Is there an easy way to create ordinals in C#? but I'm not sure if it's possible in Xslt.

At this point we only need an English solution.

like image 728
Tony Distler Avatar asked Jul 29 '09 19:07

Tony Distler


2 Answers

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">th</xsl:when>
      <xsl:when test="$mod10 = 1">st</xsl:when>
      <xsl:when test="$mod10 = 2">nd</xsl:when>
      <xsl:when test="$mod10 = 3">rd</xsl:when>
      <xsl:otherwise>th</xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>
like image 148
Tomalak Avatar answered Nov 15 '22 12:11

Tomalak


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>

like image 29
Alohci Avatar answered Nov 15 '22 11:11

Alohci