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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With