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

thanks! now help on alternating rows needed.

Subject: thanks! now help on alternating rows needed.
From: "Bill Carter" <bcarter2003@xxxxxxxxxxx>
Date: Sat, 07 Sep 2002 14:54:03 -0500
bill carter
thank you thank you. I now have a better understanding of the != operator and preceding.

now we get to my real problem. I am trying to alternate row colors on a table.

here is my xml:

<stocks>
  <ticker>AAA</ticker>
  <ticker>ABT</ticker>
  <ticker>BIG</ticker>
  <ticker>CBS</ticker>
  <ticker>CBS</ticker>
  <ticker>IBM</ticker>
  <ticker>XYZ</ticker>
</stocks>

this style sheet works at producing the desired results of producing a table each ticker displayed in each row.

<table>
<xsl:for-each select="stocks/ticker">
<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">white</xsl:when>
<xsl:when test="position() mod 2 = 0">yellow</xsl:when>
</xsl:choose>
</xsl:attribute>
<td><xsl:value-of select="." />: Pos:<xsl:value-of select="position()" /></td>
</tr>
</xsl:for-each>
</table>


gives me:
AAA: Pos:1
ABT: Pos:2 YELLOW BACKGROUND
BIG: Pos:3
CBS: Pos:4 YELLOW BACKGROUND
CBS: Pos:5
IBM: Pos:6 YELLOW BACKGROUND
XYZ: Pos:7


but, I don't want to display the cbs ticker twice, because it is a repeat. So I add the appropriate if.


<table>
<xsl:for-each select="stocks/ticker">
<xsl:if test="not(. = preceding-sibling::ticker)">
<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">white</xsl:when>
<xsl:when test="position() mod 2 = 0">yellow</xsl:when>
</xsl:choose>
</xsl:attribute>
<td><xsl:value-of select="." />: Pos:<xsl:value-of select="position()" /></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>


this gives me:

AAA: Pos:1
ABT: Pos:2 YELLOW BACKGROUND
BIG: Pos:3
CBS: Pos:4 YELLOW BACKGROUND
IBM: Pos:6 YELLOW BACKGROUND
XYZ: Pos:7

I have gotten rid of the duplicate, but now my alternating row colors are wrong. looks like the position() cant be used here.

okay, so I have been reading around this similar problem and see that there might be an answer with a recursive template and a parameter, but I haven't gotten too far down that road. Is this the solution? I really need a counter (parameter?) that increments each time the table row is displayed and the row color is based on that. Is there a straighforward way of doing this?

I appreciate all your help. You guys are great.

will


From: Mike Brown <mike@xxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re:  preceding...please help understanding...
Date: Sat, 7 Sep 2002 12:13:10 -0600 (MDT)

Bill Carter wrote:
> I am having trouble understanding preceding.

What you don't realize about preceding is not affecting your results.
The preceding axis picks up more nodes than preceding-sibling. You
probably want preceding-sibling. Otherwise, if you have something like

<foo>
  <stocks>
    <ticker>ZZZ</ticker>
    <ticker>AAA</sticker>
  </stocks>
  <stocks>
    <ticker>AAA</sticker>
    <ticker>BBB</sticker>
  </stocks>
</foo>

and you start at the BBB ticker, preceding::ticker picks up the first
ZZZ and AAA as well as the immediately preceding AAA.
preceding-sibling::ticker will get you just the ones you want.

What you're confused about, though, and this is what is skewing your results,
is equality comparisons on node-sets.


$set1 = $set2 is true if any node in $set1 has a string-value equal to the
string-value of any node in $set2.

$set1 != $set2 is true if any node in $set1 has a string-value not equal to
the string-value of any node in $set2. != is very rarely what you want. Use
not() and = instead, like this:

not($set1 = $set2)

...which will be true if no node in $set1 has a string-value equal to the
string-value of any node in $set2.

Similar rules apply if instead of $set1 you have any other object type;
if the operand on the right is a node-set, then the string-value of the left
operand is compared to the string-value of every node in the node-set.



- Mike ____________________________________________________________________________ mike j. brown | xml/xslt: http://skew.org/xml/ denver/boulder, colorado, usa | resume: http://skew.org/~mike/resume/

XSL-List info and archive: http://www.mulberrytech.com/xsl

_________________________________________________________________ Chat with friends online, try MSN Messenger: http://messenger.msn.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.