xsl-list
[Top] [All Lists]

Re: Alternating row background color - a twist

2004-12-28 07:31:41
Hey Hardy,

Why not use the same math logic against the district_id?

Such like:

<xsl:if test="district_id mod 2 = 0">
...do this?
</xs:if>

The following code might make your recursive problem a bit easier to
manage as well using apply-templates instead of for-each to put the
code into seperate templates that can easily be managed and modified
"outside" of the code-base it will subsequently become back together
with in the output:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
  <xsl:template match="/">
    <xsl:apply-templates select="legislators"/>
  </xsl:template>
  <xsl:template match="legislators">
    <table>
      <xsl:apply-templates select="legislator"/>
    </table>
  </xsl:template>
  <xsl:template match="legislator">
    <xsl:variable name="did">
      <xsl:choose>
        <xsl:when test="district_id mod 2 = 0">
          <xsl:text>#fff</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>#ccc</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <tr style="background:{$did}">
      <td>
        <xsl:value-of select="name"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Hope this helps!

Cheers!

<M:D/>


On Tue, 28 Dec 2004 09:13:06 -0500, Hardy Merrill
<HMerrill(_at_)dhcr(_dot_)state(_dot_)ny(_dot_)us> wrote:
The typical way of alternating the background color of every other row
in an HTML table is something like this:

<xsl:for-each select="msxsl:node-set($sorted_legislators)">
        <tr>
                <xsl:if test="position() mod 2 = 0">
                        <xsl:attribute
name="bgcolor">#eeeeee</xsl:attribute>
                </xsl:if>

------------------------------------

My situation is this - the for-each is based on legislators sorted by
district - here's a sample (not real) xml to illustrate:

<legislators>
    <legislator>
         <name>Joe</name>
         <district_id>1</district_id>
    </legislator>
    <legislator>
         <name>Bob</name>
         <district_id>2</district_id>
    </legislator>
    <legislator>
         <name>Pete</name>
         <district_id>2</district_id>
    </legislator>
</legislators>

There will be one *or more* legislators for each district.  Instead of
alternating the background color for every *legislator*, I want to
alternate the background color for every *district*.  In other words, I
want Joe in district_id 1 to have white background, but I want both Bob
and Pete in district_id 2 to have a gray background.  But since I can't
change the value of a variable (can't increment a counter), I don't know
what I can base the bgcolor alternation on.

The xsl:for-each is based on a select of a variable containing all
legislators sorted by district.

I know there is probably an easy way to do this, but I don't know it.
Help!

Thanks.

Hardy Merrill

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




-- 
:: M. David Peterson ::
XML & XML Transformations, C#, .NET, and Functional Languages Specialist

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>