[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Hi Spencer,
So you are stuck with XSTL 1.0? Let me give it a try. Someone already pointed out that 11..13 are exceptions to the mod 10 rule. This is the simplest I could think of and it works up to 1000 at least (didn't check every number though) and it may even work for higher numbers. Lucky thing: not a huge <xsl:choose>, just a tiny one.
Set it to any value to extend the list. Every number from 1 to $top is output. You prob. only need the tiny piece that is called "ordinal". The stylesheet works with any input, as it does not do anything with the input.
Have fun with it ;)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/" >
<ordinals>
<xsl:call-template name="sequence">
<xsl:with-param name="top" select="200" />
</xsl:call-template>
</ordinals>
</xsl:template>
<!-- returns a node set of ord-nodes with all
numbers up to a given top -->
<xsl:template name="sequence" >
<xsl:param name="top" />
<xsl:if test="$top > 1" >
<xsl:call-template name="sequence">
<xsl:with-param name="top" select="$top - 1" />
</xsl:call-template>
</xsl:if>
<ord>
<xsl:value-of select="$top" />
<xsl:call-template name="ordinal">
<xsl:with-param name="number" select="$top" />
</xsl:call-template>
</ord>
</xsl:template>
<!-- gets ordinal of a number in english -->
<xsl:template name="ordinal">
<xsl:param name="number" />
<xsl:choose>
<xsl:when test="$number mod 100 = 11">th</xsl:when>
<xsl:when test="$number mod 100 = 12">th</xsl:when>
<xsl:when test="$number mod 100 = 13">th</xsl:when>
<xsl:when test="$number mod 10 = 3">rd</xsl:when>
<xsl:when test="$number mod 10 = 2">nd</xsl:when>
<xsl:when test="$number mod 10 = 1">st</xsl:when>
<xsl:otherwise>th</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Steve wrote:
Re: [xsl] Numbering 1st 2nd 3rd ...
Subject: Re: [xsl] Numbering 1st 2nd 3rd ... From: Abel Braaksma <abel.online@xxxxxxxxx> Date: Wed, 13 Sep 2006 00:22:14 +0200 |
Hi Spencer,
So you are stuck with XSTL 1.0? Let me give it a try. Someone already pointed out that 11..13 are exceptions to the mod 10 rule. This is the simplest I could think of and it works up to 1000 at least (didn't check every number though) and it may even work for higher numbers. Lucky thing: not a huge <xsl:choose>, just a tiny one.
Mark the line that says: <xsl:with-param name="top" select="200" />
Set it to any value to extend the list. Every number from 1 to $top is output. You prob. only need the tiny piece that is called "ordinal". The stylesheet works with any input, as it does not do anything with the input.
Have fun with it ;)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/" >
<ordinals>
<xsl:call-template name="sequence">
<xsl:with-param name="top" select="200" />
</xsl:call-template>
</ordinals>
</xsl:template>
<!-- returns a node set of ord-nodes with all
numbers up to a given top -->
<xsl:template name="sequence" >
<xsl:param name="top" />
<xsl:if test="$top > 1" >
<xsl:call-template name="sequence">
<xsl:with-param name="top" select="$top - 1" />
</xsl:call-template>
</xsl:if>
<ord>
<xsl:value-of select="$top" />
<xsl:call-template name="ordinal">
<xsl:with-param name="number" select="$top" />
</xsl:call-template>
</ord>
</xsl:template>
<!-- gets ordinal of a number in english -->
<xsl:template name="ordinal">
<xsl:param name="number" />
<xsl:choose>
<xsl:when test="$number mod 100 = 11">th</xsl:when>
<xsl:when test="$number mod 100 = 12">th</xsl:when>
<xsl:when test="$number mod 100 = 13">th</xsl:when>
<xsl:when test="$number mod 10 = 3">rd</xsl:when>
<xsl:when test="$number mod 10 = 2">nd</xsl:when>
<xsl:when test="$number mod 10 = 1">st</xsl:when>
<xsl:otherwise>th</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Cheers, Abel Braaksma http://abelleba.metacarpus.com
Steve wrote:
Recursive algorithm? (template which starts at the first, and calls itself passing param = param + 1).
-s
On 9/12/06, Spencer Tickner <spencertickner@xxxxxxxxx> wrote:Hi List,
Thanks in advance for the help. I'm wondering if there's an elegant way to format position() like: 1st 2nd 3rd and so on. I've looked through google for a format that works with xsl:number, but haven't been able to find anything. I know I could always do a long <xsl:choose> statement, but it feels wrong. Anyway here's some samples
XML
<root> <num>foo</num> <num>bar</num> </root>
Output
<root> <num>1st</num> <num>2nd</num> </root>
Cheers,
Spencer
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Numbering 1st 2nd 3rd ..., Steve | Thread | Re: [xsl] Numbering 1st 2nd 3rd ..., Spencer Tickner |
Re: [xsl] XSL:FO - how to render a , G. Ken Holman | Date | Re: [xsl] Numbering 1st 2nd 3rd ..., Spencer Tickner |
Month |