You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Didier VILLEVALOIS <dv...@techmetrix.net> on 2000/05/24 15:01:13 UTC

RE: problems with variable - variables are only visible when scop ed

Can't we put xsl:choose inside xsl:variable ? Something like this :

<xsl:variable name="title">
  <xsl:choose>
    <xsl:when test="language[. = d]">Arhhh Nein!!</xsl:when>
    <xsl:when test="language[. = e]">Ohh No!!</xsl:when>
    <xsl:otherwise>Bah Si!!</xsl:otherwise>
  <xsl:choose>
</xsl:variable>

<xsl:copy-of select="$title"/>

I don't know if it works...
Didier.

-----Message d'origine-----
De: Shane_Curcuru@lotus.com [mailto:Shane_Curcuru@lotus.com]
Date: mercredi 24 mai 2000 15:03
À: xalan-dev@xml.apache.org
Objet: Re: problems with variable - variables are only visible when
scoped

(Ok, that wasn't a great Subject line, haven't had my morning coffee yet
8-)

This is correct behavior.  See:  http://www.w3.org/TR/xslt#local-variables
which details how variables are scoped (i.e. where a variable is visible
depending on where you declare it).  Basically your xsl:variable
declarations are only visible within the individual xsl:when statements,
and not outside of them.  Also, don't forget that you cannot re-bind or
re-set the value of a variable - they're more like constants.

Anyone else have a good suggestion for how to do something like this?  This
is a fairly common question, there might be other ideas in the mailing list
archive at  http://xml-archive.webweaving.org/xml-archive-xalan/

---- you <le...@post.ch> wrote: ----
> the following code won't work.....
>
> <xsl:template match="/" name="Main">
> <xsl:choose>
>  <xsl:when test="Language[. = 'd']">
>   <xsl:variable name="Title">uebersicht</xsl:variable>
>  </xsl:when>
...
> </xsl:choose>
>
> <H1><xsl:value-of select="$Title"/></H1>
> </xsl:template>
>
> xalan says:
>
> XSL Error: pattern = '$Title'
> VariableReference given for variable out of context or without
definition!

----    ----
- Shane               Automation & Test guy
mailto:shane_curcuru@lotus.com  AIM:xsltest
  http://alphaworks.ibm.com/tech/LotusXSL
  http://xml.apache.org/xalan