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

RE: Problems incrementing a variable in a <xsl:for-eac

Subject: RE: Problems incrementing a variable in a <xsl:for-each>
From: "Richard Lander" <rlander@xxxxxxxxxxxxx>
Date: Wed, 23 Oct 2002 07:32:03 -0700
increment xslt
Morning,
 
XSLT is somewhat difficult to adopt given that many programming idioms don't apply. You've run into this problem with attempting to increment a variable in the way you might in C# or Java. The trick in XSLT is to do things a little differently.
 
You cannot do what you are doing for two reasons:
- variables only exist within the scope that they are declared
- variables (and params) are immutable
 
First, you have to ask yourself the question of why you are incrementing a variable. It looks like you want to determine the number of clients in your file. You can do that via the count function.
 
Try modifying your transform in the following way:
 
<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform' <http://www.w3.org/1999/XSL/Transform'> >
<xsl:output method='text'/>

<xsl:variable name="counter" select="0"/>
<xsl:template match='CLIENTS'> 
    <xsl:value-of select="count(CLIENT)"/>
</xsl:template>
</xsl:stylesheet>

Thanks,

Rich


	-----Original Message----- 
	From: Carlos Barroso [mailto:est-c-barroso@xxxxxxxxxxxxx] 
	Sent: Wed 23/10/2002 6:42 AM 
	To: XSL-List-Digest@xxxxxxxxxxxxxxxxxxxxxx 
	Cc: 
	Subject:  Problems incrementing a variable in a <xsl:for-each>
	
	

	Hy there.
	I'm having problems incrementing a variable in a <xsl:for-each> section.
	Below is the XML document, the stylesheet I used and the output I got.
	
	-----------------------
	XML file
	-----------------------
	
	<?xml version='1.0' encoding='ISO-8859-1'?>
	<!DOCTYPE CLIENTS SYSTEM 'counter.dtd'>
	
	<CLIENTS>
	  <CLIENT>
	    <NAME value='xpto'/>
	    <AGE value='10'/>
	  </CLIENT>
	  <CLIENT>
	    <NAME valor='xxx'/>
	    <AGE valor='20'/>
	  </CLIENT>
	</CLIENTS>
	
	----------------------
	First XSL file
	----------------------
	
	<?xml version='1.0' encoding='ISO-8859-1'?>
	
	<xsl:stylesheet version='1.0'
	  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
	<xsl:output method='text'/>
	
	<xsl:variable name="counter" select="0"/>
	<xsl:template match='/CLIENTS'> 
	  <xsl:for-each select="./CLIENT">
	    <xsl:variable name="counter" select="1 + $counter"/>
	    <xsl:value-of select="$counter"/>
	  </xsl:for-each>
	</xsl:template>
	</xsl:stylesheet>
	
	-------------------------
	Output in TXT file
	-------------------------
	
	11
	
	-------------- // --------------
	
	------------------------
	Second XSL test
	------------------------
	
	<?xml version='1.0' encoding='ISO-8859-1'?>
	
	<xsl:stylesheet version='1.0'
	  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
	<xsl:output method='text'/>
	
	<xsl:variable name="counter" select="0"/>
	<xsl:template match='/CLIENTS'> 
	    <xsl:variable name="counter" select="1 + $counter"/>
	    <xsl:value-of select="$counter"/>
	    <xsl:variable name="counter" select="1 + $counter"/>
	    <xsl:value-of select="$counter"/>
	</xsl:template>
	</xsl:stylesheet>
	
	-------------------------
	Output in TXT file
	-------------------------
	
	12
	
	-------------- // --------------
	
	In the second XSL file, the counter procedes as planned. But in the first
	XSL the counter mantains
	it's value!? I tried using templates instead of "<xsl:for-each>" but it
	gives me the same results!
	I don't know what's wrong.
	Can someone help me please. This is very weird to me.
	Thanks a lot.
	
	
	 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.