RE: Variable Scoping

Subject: RE: Variable Scoping
From: Khun Yee Fung <kyeefung@xxxxxxxxxx>
Date: Wed, 3 Nov 1999 11:58:12 -0500
There is of course another twist to the story. Given the following XML file,

<?xml version='1.0'?>

And the following XSLT document.

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=''

<xsl:output method='html'/>

<xsl:template match='/'>
  <xsl:variable name='var' select='0'/>
  <xsl:for-each select='top/second'>
    <xsl:variable name='var' select='$var + number(.)'/>
    <second><xsl:value-of select='.'/></second>
    <xsl:if test='position() = last()'><xsl:value-of


I tried SAXON, XT, and Lotus XSLT Processors. The results are as follow.

% [11:32:49] > java com.lotus.xsl.Process -in var.xml -xsl var.xsl
========= Parsing file:var.xsl ==========
Parse of file:var.xsl took 931 milliseconds
========= Parsing file:var.xml ==========
Parse of file:var.xml took 100 milliseconds
<!DOCTYPE top PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
transform took 251 milliseconds
XSLProcessor: done

% [11:33:23] > xt var.xml var.xsl

% [11:33:32] > saxon var.xml var.xsl
Error processing source document
At xsl:variable on line 10 of file:var.xsl: Local variable var is already
Elapsed time: 440 milliseconds

The specification (section 11.5 of the October PR) seems to say that the
SAXON result is the correct one. It says 'It is an error if a binding
established by an xsl:variable or xsl:param element within a template
shadows another binding established by an xsl:variable or xsl:param element
also within the template'. In fact, no two bindings can occur within the
same template. This includes bindings within the same element for sure.

Personally I consider the argument that a variable as in Java or C++ makes
it harder to create an implementation "that processes a document other than
in a batch-like way, starting at the beginning and continuing through to the
end" to be bogus. I personally think that the purpose of designing a
programming language is to make the life of the developer easier, not to
make the implementation of the language easier. Unless, of course, XSLT is
not a programming language.

Khun Yee

		-----Original Message-----
		From:	Clark C. Evans
		Sent:	Wednesday, November 03, 1999 10:14 AM
		To:	xsl-list@xxxxxxxxxxxxxxxx
		Subject:	Variable Scoping 

		$ cat test.xml 

		$ cat test.xsl 
		                xmlns:data="any-uri" > 
		  <xsl:template match="/" >
		    <xsl:variable name="x">Outer</xsl:variable>
		    <xsl:for-each select="*">
		       <xsl:variable name="x">Inner</xsl:variable>
		       <xsl:value-of select="$x" />

		$ saxon test.xml test.xsl Error
		processing source document At xsl:variable on line 8 of
		file:/home/clark/test.xsl: Local variable x is already

		$ xt test.xml test.xsl


		Which one is correct?


		 XSL-List info and archive:

 XSL-List info and archive:

Current Thread