Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternating row colours in a tr class using XSL

Tags:

xml

xslt

I have a XSL document which has a varaible number of articles inserted into it. I need the background colours of the articles to alternate - "Odd" then "even"

<xsl:for-each select="newsletter/section/article">
    <tr class="odd" style="background-color: #efefef;">
        <td valign="top">
            <xsl:element name="a">
                <xsl:attribute name="href">
                    <xsl:value-of select="link" />
                </xsl:attribute>
                <img align="left" valign="top" width="110" 
                            style="padding: 0 4px 4px 0; border:0;">
                    <xsl:attribute name="alt">
                        <xsl:value-of select="title" />
                    </xsl:attribute>
                    <xsl:attribute name="src">
                        <xsl:value-of select="img" />
                    </xsl:attribute>
                </img>
            </xsl:element>
        </td>
        <td valign="top" style="padding: 4px 4px 18px 0;">
            <strong>
                <xsl:element name="a">
                    <xsl:attribute name="href">
                        <xsl:value-of select="link" />
                    </xsl:attribute>
                    <xsl:value-of select="title"/>
                </xsl:element>
            </strong>
            <br />
            <xsl:value-of select="excerpt"/>
        </td>
    </tr>
</xsl:for-each>

Ive looked at this post: HTML table with alternating row colors via XSL

but my case is different I believe. I just need to change the tr class on each iteration. Sorry for the weird formatting, I seem to be having problems pasting code in Chrome on here.

like image 591
BobFlemming Avatar asked Feb 10 '11 16:02

BobFlemming


2 Answers

Use:

<xsl:for-each select="newsletter/section/article">
  <xsl:variable name="vColor">
    <xsl:choose>
      <xsl:when test="position() mod 2 = 1">
        <xsl:text>#efefef</xsl:text>
      </xsl:when>
      <xsl:otherwise>#ababab</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>


  <tr class="odd" style="background-color: {$vColor};">
like image 52
Dimitre Novatchev Avatar answered Nov 15 '22 01:11

Dimitre Novatchev


<xsl:for-each select="date">
    <tr>
        <xsl:if test="position() mod 2 = 1">
            <xsl:attribute name="class">odd</xsl:attribute>
            <xsl:attribute name="style">background-color: #efefef;"
            </xsl:attribute>
        </xsl:if>
        <td valign="top">
            <a href="{link}">
                <img align="left" valign="top" width="110"
                            style="padding: 0 4px 4px 0; border:0;"
                            alt="{title}"
                            src="{img}"/>
            </a>
        </td>
        <td valign="top" style="padding: 4px 4px 18px 0;">
            <strong>
                <a href="{link}">
                    <xsl:value-of select="title"/>
                </a>
            </strong>
            <br />
            <xsl:value-of select="excerpt"/>
        </td>
    </tr>
</xsl:for-each>
like image 23
Flack Avatar answered Nov 14 '22 23:11

Flack