This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: how do you redefine a variable
- To: "Daniel Newman" <daniel dot newman at bis-web dot net>
- Subject: Re: [xsl] how do you redefine a variable
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Tue, 27 Feb 2001 17:39:40 +0000
- CC: xsl-list at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <NEBBJGCDKMLHOAAONGKDGEKHCAAA.daniel.newman@bis-web.net>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Daniel,
> I thought about getting the position() value, and doing something
> with this, but it would ultimately need to be a 0 or 1 (or 1 or 2 if
> that's easier). Is there an If odd(position()), if even(position())
> syntax I could use?
No, but you can look at the position() mod 2. If the position() mod 2
is 0 then it's an even row, if it's 1 then it's an odd row. So you
can do:
<xsl:for-each select="...">
<xsl:choose>
<xsl:when test="position() mod 2">
<xsl:variable name="bgcolor">red</xsl:variable>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="bgcolor">blue</xsl:variable>
</xsl:otherwise>
</xsl:choose>
...
</xsl:for-each>
[Note: test='position() mod 2' is exactly the same as
test='boolean(position() mod 2)' which is exactly the same as
test='(position() mod 2) != 0' which in is exactly the same as
test='(position() mod 2) = 1', i.e. it's testing if the row is odd.]
However, I don't think this is what you want to do exactly because
once you get outside the xsl:when or the xsl:otherwise, the variable
$bgcolor goes out of scope. I think you want to set the variable
bgcolor differently in the two circumstances, right? In that case,
you need to define the variable with the xsl:choose inside it:
<xsl:for-each select="...">
<xsl:variable name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 2">red</xsl:when>
<xsl:otherwise>blue</xsl:otherwise>
</xsl:choose>
</xsl:variable>
...
</xsl:for-each>
Or you could get really hacky and use:
<xsl:variable name="bgcolor"
select="substring('blue red',
(position() mod 2) * 5 + 1, 4)" />
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list