[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: how do you redefine a variable

Subject: Re: how do you redefine a variable
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 27 Feb 2001 17:39:40 +0000
xsl variable redefine
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.