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

RE: Variable Scoping

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

<?xml version='1.0'?>
<top>
  <second>1</second>
  <second>2</second>
  <second>3</second>
  <second>4</second>
  <second>5</second>
  <second>6</second>
</top>

And the following XSLT document.

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>

<xsl:output method='html'/>

<xsl:template match='/'>
  <top>
  <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
select='$var'/></xsl:if>
  </xsl:for-each>
  </top>
</xsl:template>

</xsl:stylesheet>

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

Lotus
-------
% [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
=============================
Transforming...
<!DOCTYPE top PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<top>
<second>1</second>
<second>2</second>
<second>3</second>
<second>4</second>
<second>5</second>
<second>6</second>21</top>
transform took 251 milliseconds
XSLProcessor: done


XT
---
% [11:33:23] > xt var.xml var.xsl
<top><second>1</second><second>2</second><second>3</second><second>4</second
><second>5</second><second>6</second>6</top>

SAXON
----------
% [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
declared
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
[mailto:clark.evans@xxxxxxxxxxxxxxxxxxxx]
		Sent:	Wednesday, November 03, 1999 10:14 AM
		To:	xsl-list@xxxxxxxxxxxxxxxx
		Subject:	Variable Scoping 

		$ cat test.xml 
		<parent>
		  <child/> 
		</parent> 

		$ cat test.xsl 
		<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		                version="1.0"
		                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" />
		    </xsl:for-each> 
		  </xsl:template>
		</xsl:stylesheet> 

		$ 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
declared 

		$ xt test.xml test.xsl
		Inner

		...

		Which one is correct?

		Clark


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


 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.