Translation, decoding in xslt

Lookup

1. How to create a decode like function
2. lookup table problems
3. static lookup table inside my xsl stylesheet
4. Associative Arrays

1.

How to create a decode like function

James Clark

One way is to create a file month.xml like this:

<months>
<month>Jan</month>
<month>Feb</month>
<month>Mar</month>
<month>Apr</month>
<month>May</month>
<month>Jun</month>
<month>Jul</month>
<month>Aug</month>
<month>Sep</month>
<month>Oct</month>
<month>Nov</month>
<month>Dec</month>
</months>


and then use

<xsl:value-of
  select="document('month.xml')
           /months/month[number
	(substring('19991003',5,2))]"/>

This approach also makes it easy to localize your XSL
stylesheet for different languages.

A less general approach is something like this:

<xsl:template name="month">
  <xsl:param name="n" select="0"/>
  <xsl:value-of 
	select="substring('JFMAMJJASOND',$n,1)"/>
  <xsl:value-of 
	select="substring('aeapauuuecoe',$n,1)"/>
  <xsl:value-of 
	select="substring('nbrrynlgptvc',$n,1)"/>
</xsl:template>

            

2.

lookup table problems

Steve Tinney

I typically approach lookup table problems of this kind like this:

class.xsl:
- ----------

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="class">
  <c code="P" means="PRIORITY"/>
  <c code="F" means="FAST"/>
  <c code="R" means="ROUTINE"/>
  <c code="S" means="SLOW"/>
</xsl:variable>

<xsl:template name="get-secur-class">
  <xsl:param name="code" select="//@secur.classif"/>
  <xsl:value-of 
    select="document('')/*/xsl:variable[@name='class']
              /c[@code=$code]/@means"/>
</xsl:template>

</xsl:stylesheet>
test.xsl:
- ---------

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:include href="class.xsl"/>

<xsl:template match="/">
  <xsl:variable name="secur.class">
    <xsl:call-template name="get-secur-class"/>
  </xsl:variable>
  <xsl:message
   >Class = <xsl:value-of select="$secur.class"/></xsl:message>
</xsl:template>

</xsl:stylesheet>

The '//' on the select of secur.classif, by the way, is a recipe for slow performance, but perhaps you have no other choice.

3.

static lookup table inside my xsl stylesheet

J.Pietschmann



  > Can I have a static lookup table *inside* my xsl stylesheet?

Yes.

> Something like

> <xsl:stylesheet>

>  <definitions>
>   <term name="GMT">Greenwich Mean Time</term>
>   <term name="MST">Mountain Standard Time</term>
>  </definitions>

>  <xsl:template>
>   <xsl:value-of select="document()//definitions/term[@name='GMT']"/>
>  </xsl:template>

Some corrections: 1. elements which don't belong to the XST namespace are allowed as direct children of xsl:stylesheet as long as they are put into another namespace 2. the stylesheet itself is adressed as document('')

Therefore, try

<xsl:stylesheet  version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:data="urn:some.urn"
   exclude-result-prefixes="data">

   <data:definitions>
     <term name="GMT">Greenwich Mean Time</term>
     <term name="MST">Mountain Standard Time</term>
   </data:definitions>

   <xsl:template>
     <xsl:value-of select="document('')/*/data:definitions[@name='GMT']"/>
   </xsl:template>
  ...

4.

Associative Arrays

David Carlisle




 >  Sorry for the (stupid ?) question, however, is there any provision 
 > for associative arrays in XSLT ?
 

XSLT doesn't do arrays of any sort. It does however do XML which is much more fun, and allows more interesting structure than an array which is of necessity rather rectangular.

if you have a map.xml that looks like

 
 <foo>
   <bar in="name1" out="MyName"/>
   <bar in="int4" out="finalInteger"/>
  ...
 </foo>

Then you can do

 
 <value-of
 select="document('map.xml')/foo/bar[@in=name(current())]/@out"/>
 
 > I am wondering if
 >  any has example hash functions already lingering around  their 
 > computer.

If the map file is big and you want the system to use a hashed lookup use a key, map file as above but use

 
 <xsl:key name="map" match="bar" use="@in"/> 
 <xsl:for-each 
   select="document('map.xml')">  
   <xsl:value-of select="key('map',name())/@out"/> 
 </xsl:for-each>