[xsl] Using a parameter in a condition

Subject: [xsl] Using a parameter in a condition
From: Ian Lang <ianplang@xxxxxxxxx>
Date: Mon, 23 Feb 2004 23:11:59 -0800 (PST)
I am implementing a look up table for localizable
strings.  I have a string table that looks like this:
<?xml version="1.0"?>
<strings>
  <string key="String.One">First String</string>
  <string key="String.Two">Second String</string>
</strings>

I do this to load the string table in my main xslt:
  <!-- Load the string table -->
  <xsl:variable name="stringTable"
select="document($stringTableFileName)"/>
where $stringTableFileName is a parameter to the style
sheet.

To test things I used variables like this:
<xsl:variable name="title"
select="$stringTable/strings/string[@key='String.One']"/>

Which worked but I wanted to instead call a template
passing it the key so I can centralize the lookup and
provide the ability to return a string "!<key>!" if
the key could not be found.  So I wrote:
  <xsl:template name="getLocalizedString">
    <xsl:param name="key"/>
    <xsl:variable name="lookupString"
select="$stringTable/strings/string[@key=$key]"/>
    <xsl:choose>
      <xsl:when test="lookupString != ''">
        <!-- Return the found string -->
        <xsl:value-of select="$lookupString"/>
      </xsl:when>
      <xsl:otherwise>
        <!-- Return a string indicating what key could
not be found -->
        <xsl:text>!</xsl:text><xsl:value-of
select="$key"/><xsl:text>!</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

The trouble is that no matter how I write the
definition for lookupString and no matter how I pass
the parameter key I cannot get it to work.  I either
get returns of "!!" or "!<key>!" but never the string
I want.  This seems like and FAQ but I cannot seemt to
find what I am looking for.

Any help apprciated,

IL



__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools

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


Current Thread