Re: Retaining value of a Global variable
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.
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format