Re: [xsl] variable scope and test directives

Subject: Re: [xsl] variable scope and test directives
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 20 Feb 2003 01:50:56 -0700 (MST)
Terence Kearns wrote:
> Is it just my Sablotron processor, or are variables declared inside
> xsl:choose trapped inside the "choose" scope(?)?
> 
> That seems useless. It means I can't do things like.
> 
> <xsl:choose>
>     <xsl:when test="/app/presentation/series_id='0'">
>         <xsl:variable name="defaultOption">
>             <option value="0">&#xa0;</option>
>         </xsl:variable>
>     </xsl:when>
>     <xsl:otherwise>
>         <xsl:variable name="defaultOption" select="''" />
>     </xsl:otherwise>
> </xsl:choose>

Think declarative, not imperative. $defaultOption *is* (choice of: a, b)...

<xsl:variable name="defaultOption">
  <xsl:choose>
    <xsl:when ...
etc.

Note that $defaultOption will be a result tree fragment. If you were planning
to <xsl:copy-of select="$defaultOption"/>, it will probably work, but there's
also probably a better way of achieving what you want. Show some sample input
and output and we'll give you a more elegant solution.

> is this just crappy behavior or is there a valid reasons for
> considdering a test condition as variable scope boundaries?

It has nothing to do with the test, really. Scope is determined by the
position of the xsl:variable element within the stylesheet tree (the
stylesheet is parsed into an XPath/XSLT node tree just like the source doc).
XPath expressions occurring in following siblings and their descendants can
use the binding.

> Surely no other language does this - because it's, er, stupid!

This is the world of functional, not procedural, programming. Read up on LISP
(ever get fancy with an Emacs config file?), Scheme or Haskell, and brace
yourself, because sooner or later you're going to be using recursion as well.

Mike

-- 
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/

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


Current Thread