Re: [xsl] Hash / Translation Tables (the right way)

Subject: Re: [xsl] Hash / Translation Tables (the right way)
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Mon, 26 Sep 2011 10:03:59 +0100
On 25 September 2011 22:28, Hank Ratzesberger <hankr@xxxxxxxx> wrote:
> Hi
>
> I know that XSLT code can be a verbose, but often enough I
> find that it's because I'm not using the language as
> intended, so I'm asking for your thoughts.
>
> I'm having some problems with constructs like hash tables or
> indexes, which are quick in other languages but presently I
> am using some brute force with.  E.G.
>
>
> <xsl:template name="month-of">
>  <xsl:param name="mon"/>
>    <xsl:choose>
>      <xsl:when test="lower-case($mon) = 'jan'">
>        <xsl:value-of select="'01'"/>
>      </xsl:when>
>      <xsl:when test="lower-case($mon) = 'feb'">
>        <xsl:value-of select="'02'"/>
>      </xsl:when>
>      <xsl:when test="lower-case($mon) = 'mar'">
>        <xsl:value-of select="'03'"/>
>      </xsl:when>
>
> It seems there must be a way to accomplish this with
> a simple sequence. Months of the year, days of the
> week, and other one to one translations from the
> textual to ordinal or vice versa.

The simple sequence way is:

('jan', 'feb', 'mar' ....)[number($mon)]

...and if the param $mon was typed as a number already you could drop
the number() function.

In practice though, you wouldn't want to repeatedly create the
sequence, and you'd want to check the $mon param is between 1 and 12,
so you would need to separate it out a bit.

--
Andrew Welch
http://andrewjwelch.com

Current Thread