xsl-list
[Top] [All Lists]

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

2011-09-25 20:09:21
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(_at_)ucsd(_dot_)edu> 
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.

Cheers,
Hank

--
Louis (Hank) Ratzesberger
sopac.ucsd.edu

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--