[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] Hash / Translation Tables (the right way)
Subject: Re: [xsl] Hash / Translation Tables (the right way) From: Dimitre Novatchev <dnovatchev@xxxxxxxxx> Date: Sun, 25 Sep 2011 18:08:49 -0700 |
Whenever you need a "HashTable" or "Dictionary" or "Map", try to use keys: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="my:Months"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <m:Months> <m name="jan" mid="1"/> <m name="feb" mid="2"/> <m name="mar" mid="3"/> <m name="apr" mid="4"/> <m name="may" mid="5"/> <m name="jun" mid="6"/> <m name="jul" mid="7"/> <m name="aug" mid="8"/> <m name="sep" mid="9"/> <m name="oct" mid="10"/> <m name="nov" mid="11"/> <m name="dec" mid="12"/> </m:Months> <xsl:key name="kIdFromName" match="@mid" use="../@name"/> <xsl:variable name="vUpper" select= "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/> <xsl:variable name="vLower" select= "'abcdefghijklmnopqrstuvwxyz'"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="m/text()"> <xsl:variable name="vCur" select="."/> <xsl:for-each select="document('')"> <xsl:value-of select= "key('kIdFromName', translate($vCur,$vUpper,$vLower) )"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> when this XSLT 1.0 transformation ()as you indicated you are limited to XSLT 1.0) is applied on the following document: <t> <m>Mar</m> <m>Jun</m> <m>Aug</m> <m>Oct</m> <m>May</m> <m>Jan</m> <m>Dec</m> <m>Nov</m> <m>Sep</m> <m>Jul</m> <m>Feb</m> <m>Apr</m> </t> the wanted, correct result is produced: <t> <m>3</m> <m>6</m> <m>8</m> <m>10</m> <m>5</m> <m>1</m> <m>12</m> <m>11</m> <m>9</m> <m>7</m> <m>2</m> <m>4</m> </t> -- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk ------------------------------------- Never fight an inanimate object ------------------------------------- You've achieved success in your field when you don't know whether what you're doing is work or play ------------------------------------- Facts do not cease to exist because they are ignored. ------------------------------------- I finally figured out the only reason to be alive is to enjoy it. On Sun, Sep 25, 2011 at 2:28 PM, 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. B E.G. > > > <xsl:template name="month-of"> > B <xsl:param name="mon"/> > B B <xsl:choose> > B B B <xsl:when test="lower-case($mon) = 'jan'"> > B B B B <xsl:value-of select="'01'"/> > B B B </xsl:when> > B B B <xsl:when test="lower-case($mon) = 'feb'"> > B B B B <xsl:value-of select="'02'"/> > B B B </xsl:when> > B B B <xsl:when test="lower-case($mon) = 'mar'"> > B B B B <xsl:value-of select="'03'"/> > B B B </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. > > Cheers, > Hank > > -- > Louis (Hank) Ratzesberger > sopac.ucsd.edu
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Hash / Translation Tables, G. Ken Holman | Thread | Re: [xsl] Hash / Translation Tables, Andrew Welch |
Re: [xsl] Hash / Translation Tables, Hank Ratzesberger | Date | RE: [xsl] Client-side XSLT, Tom T |
Month |
Keywords