Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 30th, 2007, 03:13 PM
Authorized User
 
Join Date: Mar 2007
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Default "Out of scope" var???

Hey,

This is one of these problems that simply doesnt make sense for me.
Look at this SIMPLE sample package below:

<?xml version="1.0" encoding="UTF-8" ?>
<package debug="true">
<PackageTransform>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="http://www.aspdotnetstorefront.com">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="1=1">
<xsl:variable name="myvar">Hello world!</xsl:variable>
<xsl:value-of select="$myvar"/>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
</PackageTransform>
</package>

Notice that within the choose tag I set a variable called "myvar" and then print it imediately. Running this script it will show "Hello world!" in the screen.

Ok, if I move this line to after the </xsl:choose> tag (see the modified script below) I got a "The variable or parameter 'myvar' is either not defined or it is out of scope." error.

<?xml version="1.0" encoding="UTF-8" ?>
<package debug="true">
<PackageTransform>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="http://www.aspdotnetstorefront.com">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="1=1">
<xsl:variable name="myvar">Hello world!</xsl:variable>
</xsl:when>
</xsl:choose>
<xsl:value-of select="$myvar"/>
</xsl:template>
</xsl:stylesheet>
</PackageTransform>
</package>

It doesnt make sense for me. Any clues?

Thanks!

 
Old March 30th, 2007, 05:16 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

I believe you are getting this error because the variable is defined when test="1=1", if that is not met, than $myvar is not assigned a value.



 
Old March 31st, 2007, 03:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In this respect XSLT is exactly like other block structured languages, you would get the same error in C or Java if you did

if (2=2) {
  int x = 3;
}
print(x);

The answer is to use

<xsl:variable name="x">
  <xsl:choose>
    <xsl:when test="2=2">
      <xsl:value-of select="3"/>
    </xsl:when>
  </xsl:choose>
</xsl:variable>

or in 2.0

<xsl:variable name="x" select="if (2=2) then 3 else 0"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
var,text ,..... angelboy SQL Language 2 May 29th, 2007 11:37 AM
var as variable incredi-man Javascript 2 September 16th, 2006 11:46 AM
Using a var into the name of another var or field. guizero Access VBA 3 May 30th, 2006 03:47 PM
Dissapearing VAR topshed Classic ASP Databases 5 June 3rd, 2005 01:34 PM
Temp Var in For Each pbernardo XSLT 5 November 14th, 2003 06:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.