Subject: RE: [xsl] Increment a variable From: "Michael Kay" <mike@xxxxxxxxxxxx> Date: Thu, 18 Aug 2005 09:30:29 +0100 |
Let's see if I can try to add some clarity - you've been given a muddled set of responses. I'll start by saying that I don't understand C++, for example I've no idea whether "var" and "Var" in your code below are the same variable. Firstly, it's correct that XSLT is a functional programming language and does not have mutable variables. Things that are achieved in other languages using iteration and control variables are achieved in a functional language using recursive calls with parameters. However, reverse-engineering your code, it's clear that your requirement is not the low-level requirement to increment a variable, but a higher-level requirement something like this: do 10 times { something do 100 times { something } something } In XSLT 2.0 you can code that pretty directly as: <xsl:for-each select="1 to 10"> ... <xsl:for-each select="1 to 100"> ... </xsl:for-each> ... </xsl:for-each> and you can access the current position in the loop using the position() function. In XSLT 1.0 that useful construct isn't available, instead you have to use one of two approaches. The clean functional approach is to replace the loops by recursive template calls. You write a template that does "something", and give it a parameter that says how many times to do it. The template does it once, and then calls itself setting the parameter to N-1, and terminating (by doing nothing) when N is zero. But some people have a phobia about recursion, probably because it can be quite hard to debug your inevitable mistakes when you're new to it, or perhaps because of worries about performance, and they prefer the loop style. In XSLT 1.0 you can iterate over a node-set using xsl:for-each, so if you want to do something ten times you can select a set of ten arbitrary nodes and iterate over it: <xsl:for-each select="(//*)[position() <= 10]"> something </xsl:for-each> This is "trick programming", some would say it's a dirty trick, and it's a matter of personal taste whether you use it. Hope that makes things clearer. Michael Kay http://www.saxonica.com/ > -----Original Message----- > From: ChandraShekar, A [mailto:ChandraShekar.A@xxxxxxxxxxx] > Sent: 18 August 2005 05:35 > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: [xsl] Increment a variable > > Hello, > I don't know whether this question is stupid question or not? > > How can I achieve following c++ code in XSLT. > > void fun1() > { > for(int i=0;i<1000;i++) > { > fun2(i); > } > } > > void fun2(int var) > { > if ( var == 0 ) > { > var++; > // do something > } > else > { > Var++; > // do something > } > if ( var == 100 ) > { > Var = 0; > // do something > } > } > > > Thanks in advance,
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Increment a variable, Aron Bock | Thread | Re: [xsl] Increment a variable, David Carlisle |
Re: [xsl] Increment a variable, Dimitre Novatchev | Date | RE: [xsl] How to capture text?, Michael Kay |
Month |