[xsl] lookup-table thoughts (was Re: matching multiple times, outputting once?

Subject: [xsl] lookup-table thoughts (was Re: matching multiple times, outputting once?
From: Tom Myers <tommy@xxxxxxxxxxxxxx>
Date: Tue, 06 Nov 2001 13:58:12 -0500
I'm just thinking about different styles of lookup-table solutions,
in particular thinking about what Dimitre posted yesterday, recasting
that same solution in terms of xsl:key with a lookup template,
and comparing them briefly, hoping that somebody will say something 
to contribute to my education...

On Mon, 5 Nov 2001 21:43:17 -0800 (PST), Dimitre Novatchev <dnovatchev@xxxxxxxxx> 
solved Marty McKeever's problem of mapping source XML
into target XML
<emphasis bold="yes">Hello</emphasis> 
<emphasis italic="yes" bold="yes">Hello</emphasis> 
<emphasis underline="yes" italic="yes" bold="yes">Hello</emphasis>
with the stylesheet
<xsl:stylesheet version="1.0" 
   xmlns:trans="myTrans" exclude-result-prefixes="trans" 
<xsl:output indent="yes" omit-xml-declaration="yes"/>

   <in>b</in> <out>bold</out>
   <in>i</in> <out>italic</out>
   <in>u</in> <out>underline</out> 

<xsl:variable name="vTrans" select="document('')/*/trans:*"/> 
<xsl:template match="b | i | u"> 
   <xsl:apply-templates select="." mode="emph"/> 

<xsl:template match="b | i | u" mode="emph"> 
   <xsl:variable name="vEmphName" 
     select="$vTrans/in[. = name(current())]/following-sibling::out[1]"/> 
   <xsl:attribute name="{$vEmphName}">yes</xsl:attribute> 
   <xsl:apply-templates mode="emph"/> 

and I looked at that and thought it was very cool, and wondered what
class of examples it might be an instance of. And I also wondered
whether the lookup table would be better or worse if recast as xsl:key,
so I did that: I replaced the xsl:variable name="vTrans" with:

<xsl:key name="transkey" match="out" use="preceding-sibling::in[1]"/>

and then I changed the definition of xsl:variable name="vEmphName" from
a clever XPath to a simple template call as:

<xsl:variable name="vEmphName">
    <xsl:call-template name="lookup-trans">
       <xsl:with-param name="in" select="name(.)"/>

and then wrote the lookup-trans template as:

<xsl:template name="lookup-trans">
   <xsl:param name="in" select="''"/>
   <xsl:for-each select="document('')"> <!-- or whatever doc holds table -->
     <xsl:value-of select="key('transkey',$in)"/>

and I didn't change anything else...the output is identical. I do like
the fact that I can take the <trans:trans> lookup table into another
document, say transtrans.xml, and then use document('transtrans.xml')
instead of document('') in the lookup template, with no other change;
I guess if we were talking about really big lookup tables, the key
solution would be faster. On the other hand, the original is a bit
shorter; I don't know a way to justify saying that one solution is
cleaner than another. Are there other issues? Am I missing something
fundamental, as usual? (Is there something I should have been reading,
rather than posting this? That happens a lot too. Certainly I should
have been doing non-XSL things rather than any of this, anyway.)

Tom Myers

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread