[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: is the variable really out of scope??

Subject: Re: is the variable really out of scope??
From: Gunther Schadow <gunther@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 05 Dec 2002 18:48:45 -0500
xsl variable name scope
> I read through the faq and tried to find an answer to the scope issue.
> I found that the faq mentions that a variable within a choose tatement
> has a scope confined to that xsl element.  However, it does not mention
> the scope of the variables external to the xsl element.

For all I know, a variable's scope is limited to the element of which
the variable element is a direct child, and all descendents of that
element. A variable binding can never cross to a sibling or be passed to an ancestor. That's just the way it is.


But I can't quite see what your problem is, because in your code below,
you properly wrap the choose statement inside the variable statement,
so that you will have the variable in scope after that. I may be missing
something, but what you list there should work in principle.

Is there a better way for me to tackle this problem other than creating three separate templates for my choose statement?


I can only see two choose statements, one for w and one for h.
Although, you don't handle your cases according to your pseudo
code :-)

If you want only one choose statement you may find this more pleasant:

<xsl:variable name="wh">
  <xsl:choose>
    <xsl:when test="...">
       <pair w="..." h="..."/>
    </xsl:when>
    <xsl:when test="...">
       <pair w="..." h="..."/>
    </xsl:when>
    <xsl:when test="...">
       <pair w="..." h="..."/>
    </xsl:when>
    <xsl:otherwise>
       <pair w="..." h="..."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="w" select="$wh/pair/@w"/>
<xsl:variable name="h" select="$wh/pair/@h"/>

Please look specifically at the 'w' and 'h' variables and their updates based on 'k' and 'i'. To help clarify my problem, I have provided my xsl script:

   <xsl:template match="/">
       <xsl:call-template name="calcOpt" />
   </xsl:template>

   <xsl:template name="calcOpt">
       <xsl:param name="N" select="23" />

       <!--  pseudo-code
           N = value in
           s = sqrt(N)
           k = s mod 1   // fractional portion of square root
           i = s - k

           if 0 < k < 0.5
               w = i
               h = i+1
           else if k >= 0.5
               w = i+1
               h = i+1
           else
               w = i
               h = i
       -->

N = <xsl:value-of select="$N" /><br />

       <xsl:variable name="s">4.79</xsl:variable>
       s = <xsl:value-of select="$s" /><br />

       <xsl:variable name="k" select="$s mod 1" />
       k = <xsl:value-of select="$i" /><br />

       <xsl:variable name="i" select="$s - $k" />
       i = <xsl:value-of select="$k" /><br />

      <xsl:variable name="w">
          <xsl:choose>
              <xsl:when test="($k &gt; 0.5) and ($k &lt; 0.5)">
                  <xsl:value-of select="$i+1" />
              </xsl:when>
              <xsl:otherwise>
                  <xsl:value-of select="$i" />
              </xsl:otherwise>
          </xsl:choose>
      </xsl:variable>
      w = <xsl:value-of select="$w" /><br />

      <xsl:variable name="h">
          <xsl:choose>
              <xsl:when test="($k = 0)">
                  <xsl:value-of select="$i" />
              </xsl:when>
              <xsl:otherwise>
                  <xsl:value-of select="$i+1" />
              </xsl:otherwise>
          </xsl:choose>
      </xsl:variable>
      h = <xsl:value-of select="$h" /><br />
   </xsl:template>


Thanks in advance, Bix


--
Gunther Schadow, M.D., Ph.D.                    gschadow@xxxxxxxxxxxxxxx
Medical Information Scientist      Regenstrief Institute for Health Care
Adjunct Assistant Professor        Indiana University School of Medicine
tel:1(317)630-7960                         http://aurora.regenstrief.org



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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.