This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: how do you redefine a variable


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]