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:when test="position() mod 2">
      <xsl:variable name="bgcolor">red</xsl:variable>
      <xsl:variable name="bgcolor">blue</xsl:variable>

[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:when test="position() mod 2">red</xsl:when>

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 Tennison

 XSL-List info and archive:

Current Thread