Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 17th, 2014, 03:48 PM
Authorized User
 
Join Date: Mar 2014
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default changeable variables

Hello,

I need a way to store a boolean variable that I can toggle. I'm told by http://www.w3schools.com/xsl/el_variable.asp that once you create a variable (with <xsl:variable.../>) you cannot change it. But then what do I use if I want a changeable variable?

Thanks.
 
Old March 17th, 2014, 04:00 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I need a way to store a boolean variable that I can toggle.

No you don't.

I'm not just being negative, I'm stating a known theorem of computer science: anything that can be done with mutable variables can also be done with purely declarative, functional programming.

Now tell us what you really need to do, and we'll tell you how to do it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 19th, 2014, 10:08 AM
Authorized User
 
Join Date: Mar 2014
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Let's say I have a for-each loop. In that loop I'm populating one row of a table. Each iteration adds a new <TD> tag. Now there are two elements in the list I'm iterating through, either of which could go into a specific cells. Whichever of those two elements is encountered first, it goes into the table cell, and the second of the two elements gets ignored. But we don't know in advance which of the two elements will come first. So I thought I could create a flag to signal when one of the elements is encountered. When the first of the two elements is encountered that flag is set to 'true', and the element is added to the table. When the second of the two elements is encountered, it will see that the flag is set to true and ignore it. But as you know, I can't create the flag as an xsl:variable with its initial value set to 'false' and then later on change it to 'true'.

But you say that declarative functional programming can do the trick? How so?
 
Old March 19th, 2014, 11:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, for-each is not a loop, it is a mapping expression. It processes each item in the input sequence in the same way, and the processing of one item does not depend in any way on the processing of previous items; this allows parallel execution. So you can't use for-each in the way you describe, where the processing of one item depends the results of processing of other items in the sequence.

There are two ways of tackling your problem.

One is to continue using for-each. While processing each item, you need to make decisions about how to handle it by reference to the input data alone. For example, you might check whether the context item has a preceding sibling that satisfies certain conditions.

The other is to abandon xsl:for-each and use recursion: process the first item, then call yourself to process the remaining items, setting a parameter to provide the kind of information you are currently trying to hold in your mutable variable.

You'll find a whole chapter in my XSLT book on use of XSLT as a functional programming language.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 19th, 2014, 02:12 PM
Authorized User
 
Join Date: Mar 2014
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
Firstly, for-each is not a loop, it is a mapping expression. It processes each item in the input sequence in the same way, and the processing of one item does not depend in any way on the processing of previous items; this allows parallel execution.
So do you mean that item #4 (let's say) might be processed before item #2? Does it store the results of each item in a temporary structure (like a C# dictionary or something like that), and then, once finished, iterate through that structure to produce the html output?


Quote:
Originally Posted by mhkay View Post
The other is to abandon xsl:for-each and use recursion: process the first item, then call yourself to process the remaining items, setting a parameter to provide the kind of information you are currently trying to hold in your mutable variable.
I might do this. I've been researching XSL and it seems recursion is used a lot.

Quote:
Originally Posted by mhkay View Post
You'll find a whole chapter in my XSLT book on use of XSLT as a functional programming language.
Ok, this might be a good investment, but I'll have to convince the company to buy it.
 
Old March 19th, 2014, 02:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Probably most XSLT processors actually evaluate xsl:for-each as a sequential loop, but the point is that they don't have to, and with multicore processors being the norm, this is increasingly useful. My Saxon-EE processor will evaluate xsl:for-each in parallel under particular circumstances, and yes, the downside is that in general you have to buffer the output. But if you allocate threads on a round-robin basis then the chances are you won't have to buffer all of it; you only have to buffer results if one thread "overtakes" another.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables Apocolypse2005 C# 2008 aka C# 3.0 2 November 12th, 2009 10:12 AM
Variables Apocolypse2005 Visual Basic 2008 Professionals 1 July 25th, 2009 05:41 AM
using variables webgrphx Classic ASP Basics 3 April 11th, 2007 06:46 PM
control heights not changeable? ParadiseIsle Beginning VB 6 1 November 10th, 2005 02:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.