[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] how do you redefine a variable
Subject: Re: [xsl] how do you redefine a variable From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Tue, 27 Feb 2001 17:39:40 +0000 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] how do you redefine a var, Keith | Thread | RE: [xsl] how do you redefine a var, Michael Kay |
RE: [xsl] how do you redefine a var, Keith | Date | RE: [xsl] RE:"*NEVER* use for-each", Chris Bayes |
Month |