I've an html table written using xslt transformation that looks like this
<table>
<xsl:for-each select="someNode">
<xsl:if test="testThis">
<tr>
<!-- <xsl:call-template name="conditionalRowStyle"/> -->
<td>something</td>
</tr>
</xsl:if>
<tr>
<!-- <xsl:call-template name="conditionalRowStyle"/> -->
<td>this is always displayed</td>
</tr>
<xsl:if test="testThis2">
<tr>
<!-- <xsl:call-template name="conditionalRowStyle"/> -->
<td>something 2</td>
</tr>
</xsl:if>
....
</xsl:for-each>
<tr>
<!-- <xsl:call-template name="conditionalRowStyle"/> -->
<td>this is always displayed</td>
</tr>
</table>
I need a way to apply different classes oddRow/evenRow to tr elems.
<tr class="evenRow"> or <tr class="oddRow">
I tried to use a template like this after every <tr> elem
<xsl:template name="conditionalRowStyle">
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="(count(../preceding-sibling::tr) mod 2) = 0">oddrow</xsl:when>
<xsl:otherwise>evenrow</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
but this is not working. any idea?
You could probably get away with doing this in just css
tr:nth-child(odd) {
/*...*/
}
tr:nth-child(odd) {
/*...*/
}
If you cannot, you could do something like
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="(position() mod 2) != 1">
<xsl:text>evenRow</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>oddRow</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
Note that i wrote this in the SO textbox and haven't tested it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With