xsl-list
[Top] [All Lists]

RE: lookup table (was: mixed content nodes question)

2005-01-07 16:29:55

Hi Kevin,

From: Kevin Rodgers [mailto:kevin(_dot_)rodgers(_at_)ihs(_dot_)com]

I'm a novice working on my first stylesheet, which has a lookup table
that I implemented like this:

<xsl:template name="convert-language">
  <xsl:param name="code">en</xsl:param>
  <!-- See ISO 639 -->
  <xsl:choose>
    <xsl:when test="$code = 'ar'">ARABIC</xsl:when>
    <xsl:when test="$code = 'de'">GERMAN</xsl:when>
    <xsl:when test="$code = 'es'">SPANISH</xsl:when>
...
    <xsl:otherwise>
      <xsl:message terminate="no">Unrecognized language code:
<xsl:value-of select="$code"/></xsl:message>
      <xsl:value-of select="$code"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

That is accessed like this:

      <xsl:call-template name="convert-language">
        <xsl:with-param name="code"
           select="stringmb3e:doc_lang_list/mb3e:doc_lang[1]"/>
      </xsl:call-template>

Then I came across an element that had no mb3e:doc_lang_list child and
thus no mb3e:doc_lang grandchildren, which generated the "Unrecognized
language code" message (with no value for the allegedly erroneous code).
So I added this instruction before <xsl:otherwise> to simply return an
empty string in that case:

    <xsl:when test="$code = ''"></xsl:when>

I was surprised that (under Xalan 2.6.0) that didn't solve the problem
-- I still got the error message.  I got the desired behavior by
changing the call to this:

      <xsl:call-template name="convert-language">
        <xsl:with-param name="code"
           select="string(mb3e:doc_lang_list/mb3e:doc_lang[1])"/>
      </xsl:call-template>

But I obviously have a fundamental misunderstanding: I know that the
select expression in the original call returns a node-set, which the
XSLT processor apparently converts automatically to a string when
evaluating the test expression (as desired, since I get the intended
language names in my output).  So why doesn't the empty node-set match
the empty string test

Because that's what an empty _node_ would look like (e.g.,
<this-node-is-empty/>), not an empty node-set.

I'd recommend not using a named/called template in the first place, but
rather something like this:

        <xsl:template match="doc_lang_list/doc_lang">
           <xsl:choose>
                <xsl:when test=". = 'ar'">ARABIC</xsl:test>
                <!-- etc...>
                <xsl:otherwise>
                   <xsl:message terminate="no">Unrecognized language 
<xsl:value-of
select="."/></xsl:message><xsl:value-of select="."/>
                </xsl:otherwise>
           </xsl:choose>
        </xsl:template>

Then somewhere you select that node in an <xsl:apply-templates>.  If there's
no element to match, then the template doesn't get applied.  It's simpler,
too - no parameters to pass.

I'd rather learn the ropes and get an XSLT 1.0 prototype working before
delving into XSLT 2.0.

I'd suggest you delve away, unless you have some external constraint locking
you down to XSLT 1.0.  Why go to extra lengths to learn workarounds for
things that aren't even problems anymore? :-)

HTH,
~ml


--~------------------------------------------------------------------
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>
--~--



<Prev in Thread] Current Thread [Next in Thread>