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

Re: newbie variable question

Subject: Re: newbie variable question
From: "John E. Simpson" <simpson@xxxxxxxxxxx>
Date: Tue, 26 Sep 2000 14:14:53 -0400
xslt variable if end if
At 12:01 PM 09/26/2000 -0400, Les Howard wrote:
if there are any grp/type element whose contents are "DMA" I want to
print out the value of the associated name in that grp.  if there is no
grp/type element whose contents is "DMA" I want to print out the values
of the description element.
[XML:]
    <area>
       <grp>
         <type>MSA</type>
         <name>foo</type>
       </grp>
       <grp>
         <type>DMA</type>
         <name>bar</type>
       </grp>
     </area>
     <description>baz</description>

[XSLT:]
  <xsl:for-each select="area/grp">
    <xsl:if test="string(type)='DMA'">
      <xsl:value-of select="name"/>
     <xsl:variable name="ValueExists" select="'True'"/>
    </xsl:if>
  </xsl:for-each>
<xsl:if  test="$ValueExists!="'True'">
 <xsl:value-of select="description" />
</xsl:if>

but I keep getting an error:

A reference to variable or parameter 'ValueExists' cannot be resolved.
The variable or parameter may not be defined, or it may not be in scope.

The error makes sense. XSLT variables are in effect ("in scope") only within the block of code in which they're defined. Your stylesheet is structured in part like this:


   Begin for-each....
     If some condition....
       Define variable $ValueExists
     End if
   End for-each

   If some condition....
     Do something with $ValueExists
   End if

See? The if-block doesn't know anything about $ValueExists. The second, standalone if-block can't "see into" the if-block which contains the variable's definition; the variable goes out of scope when that if-block closes.

You can sometimes get around the problem by declaring the variable globally -- that is, make the <xsl:variable...> element a top-level element (child of <xsl:stylesheet>). I say sometimes because you can't always get at some information that would be readily available from within a template rule.

Might be better in your case to simply instantiate one template for type="DMA" and a different one for type!="DMA", using those expressions as predicates.

==========================================================
John E. Simpson | "If you were going to
http://www.flixml.org | shoot a mime, would you use
XML Q&A: http://www.xml.com | a silencer?" (Steven Wright)



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.