|
next
|
Subject: Global vs. Local xsl:variable Scope Author: Omri Traub Date: 28 Oct 1999 12:23 PM
|
Hi Eric,
One thing you should realize about xsl variables is that they are not variables in the sense you think they are. They can only be set once, at initialization time, and can not be changed thereafter. So the line: (xsl:variable name="flip" expr="0"/) in your local template does nothing but declare a local variable "flip", which shadows the global variable of the same name. You never use that variable, and when you return to the calling template the local binding goes away, and you are left with the global variable.
A note on param vs. variable. An xsl:param at the global level acts just like a global variable. The only difference is that you can set the value of that variable outside the stylesheet, through the eXcelon interfaces. This can be very useful for changing the behavior of the stylesheet based on how it is being called.
So, given that you want to alternate colors, I would do the following:
(ul)
(xsl:for-each select="/books/book")
(li)
(xsl:choose)
(xsl:when test="position() mod 2 = 0")
(font color="#ff0000")(xsl:value-of select="title"/)(/font)
(/xsl:when)
(xsl:otherwise)
(font color="#00ff00")(xsl:value-of select="title"/)(/font)
(/xsl:otherwise)
(/xsl:choose)
(/li)
(/xsl:for-each)
(/ul)
I select the set of books with xsl:for-each. I then choose the color based on the current book's position in the list of all books selected. I use the xsl "mod" operator to get the desired flipping effect.
Hope this helps,
- Omri
|
|
|