[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Retaining value of a Global variable


Subject: Re: [xsl] Retaining value of a Global variable
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 16 Jul 2004 18:41:25 -0400

Vivek,

At 05:04 PM 7/16/2004, you wrote:
In the second template when I print whead before the <img> it shows 'No' for all articles even if I set it to 'Yes' after the <img>. Any clues on how to retain the value of the variable?

You can't. Why do you want to?


This isn't just a flippant question. Depending on your honest response, we (well, someone: I'm shortly heading home) can give you an honest XSLT answer to your real question. It will probably be easier than trying to use a variable.

When thinking "variable" in XSLT (as David C our resident mathematician sometimes has to remind us) you should think "algebra" not "FORTRAN". In conventional algebra, the statement "x=x+1" is really meaningless (and it's a good thing too). XSLT is a functional or quasi-functional language (think of the stylesheet as a function that maps the input to an output), and most of (ideally, all) the state-maintenance, for which variables are so heavily used in other technologies, is handled under the hood by the processor. Less work for us.

So your global variable hasn't been reinitialized. Rather, when the local variable that shadows it goes out of scope, it is revealed again for what it is.

In XSLT if you *really* need a counter or something to pass state between your templates, you can use a parameter. Five times out of six, this is not necessary -- which is why we have to ask what you want to achieve with your variable, to guide you to the better answer.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


Current Thread
Keywords