I am trying to set the background color of every other Here is the classic XSLT solution: Replace: with: Replace your Finally, add this template: The complete XSLT code becomes: When applied on the provided XML document: the wanted, correct result is produced: You could do this in CSS by using However, this will not work on older browsers. I like CSS solutions for style, but for general questions about this subject and without extension function, you would need to use a push style like (Dimitre's stylesheet borrowed): I am not using a for-each loop since I am using a few templates to filter data based on the nodes
<xsl:apply-templates>
<xsl:sort select="time" data-type="number" order="descending"/>
</xsl:apply-templates>
<xsl:variable name="vrtfResult">
<xsl:apply-templates>
<xsl:sort select="time" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vrtfResult)/tr"/>
<stylesheet>
element with:<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:my="my:my" extension-element-prefixes="ext my">
<my:colors>
<c>#ffff</c>
<c>#cccc</c>
</my:colors>
<xsl:variable name="vColors"
select="document('')/*/my:colors/*"/>
<xsl:template match="tr">
<xsl:variable name="vPos" select="position()"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="bgcolor">
<xsl:value-of select="$vColors[($vPos mod 2)+1]"/>
</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:my="my:my" extension-element-prefixes="ext my">
<my:colors>
<c>#ffff</c>
<c>#cccc</c>
</my:colors>
<xsl:variable name="vColors"
select="document('')/*/my:colors/*"/>
<xsl:template match="NewDataSet">
<html>
<body>
<table width="390" style="text-align:left;">
<tr>
<th style="text-align:left;"><span style="font:20px arial;
font-weight:bold;">Agent Name</span></th>
<th style="text-align:center;"><span style="font:20px arial;
font-weight:bold;">State</span></th>
<th style="text-align:center;" ><span style="font:20px arial;
font-weight:bold;">Time</span></th>
</tr>
<xsl:variable name="vrtfResult">
<xsl:apply-templates>
<xsl:sort select="time" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vrtfResult)/tr"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="tr">
<xsl:variable name="vPos" select="position()"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="bgcolor">
<xsl:value-of select="$vColors[($vPos mod 2)+1]"/>
</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="AgentSales[State='Talking Out']">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="AgentSales/AgentName">
<td style="text-align:left;">
<span style="font:14px arial;
font-weight:bold;text-align:center;"> <xsl:value-of
select="."/></span>
</td>
</xsl:template>
<xsl:template match="AgentSales/State">
<td style="text-align:center;">
<span style="font:14px arial;
font-weight:bold;text-align:center;"> <xsl:value-of
select="."/></span>
</td>
</xsl:template>
<xsl:template match="AgentSales/time">
<td style="text-align:center;">
<span style="font:14px arial;
font-weight:bold;text-align:center;"> <xsl:value-of
select="."/></span>
</td>
</xsl:template>
<xsl:template match="AgentSales/Reason | AgentSales"/>
</xsl:stylesheet>
<NewDataSet>
<AgentSales>
<AgentName>MCCALLISTER AARON</AgentName>
<State>Talking Out</State>
<Reason />
<time>9</time>
</AgentSales>
<AgentSales>
<AgentName>APPELHANS BARRY</AgentName>
<State>Talking Out</State>
<Reason />
<time>1</time>
</AgentSales>
<AgentSales>
<AgentName>ARREDONDO KARLA</AgentName>
<State>Talking Out</State>
<Reason />
<time>0</time>
</AgentSales>
</NewDataSet>
<html>
<body>
<table width="390" style="text-align:left;">
<tr>
<th style="text-align:left;"><span style="font:20px arial; font-weight:bold;">Agent Name</span></th>
<th style="text-align:center;"><span style="font:20px arial; font-weight:bold;">State</span></th>
<th style="text-align:center;"><span style="font:20px arial; font-weight:bold;">Time</span></th>
</tr>
<tr bgcolor="#cccc">
<td style="text-align:left;"><span style="font:14px arial; font-weight:bold;text-align:center;">MCCALLISTER AARON</span></td>
<td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">Talking Out</span></td>
<td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">9</span></td>
</tr>
<tr bgcolor="#ffff">
<td style="text-align:left;"><span style="font:14px arial; font-weight:bold;text-align:center;">APPELHANS BARRY</span></td>
<td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">Talking Out</span></td>
<td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">1</span></td>
</tr>
<tr bgcolor="#cccc">
<td style="text-align:left;"><span style="font:14px arial; font-weight:bold;text-align:center;">ARREDONDO KARLA</span></td>
<td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">Talking Out</span></td>
<td style="text-align:center;"><span style="font:14px arial; font-weight:bold;text-align:center;">0</span></td>
</tr>
</table>
</body>
</html>
:nth-child([even/odd])
:tr:nth-child(even) td {
background-color: #fff;
}
tr:nth-child(odd) td {
background-color: #ccc;
}
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my"
exclude-result-prefixes="my">
<my:colors>
<c>#ffff</c>
<c>#cccc</c>
</my:colors>
<xsl:variable name="vColors" select="document('')/*/my:colors/*"/>
<xsl:template match="NewDataSet">
<html>
<body>
<table width="390" style="text-align:left;">
<tr>
<th style="text-align:left;">
<span style="font:20px arial; font-weight:bold;"
>Agent Name</span>
</th>
<th style="text-align:center;">
<span style="font:20px arial; font-weight:bold;"
>State</span>
</th>
<th style="text-align:center;" >
<span style="font:20px arial; font-weight:bold;"
>Time</span>
</th>
</tr>
<xsl:apply-templates
select="AgentSales[State='Talking Out']">
<xsl:sort select="time"
data-type="number"
order="descending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="AgentSales">
<xsl:variable name="vPos" select="position()"/>
<tr bgcolor="{$vColors[($vPos mod 2)+1]}">
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="AgentSales/*">
<xsl:variable name="vAlign"
select="concat(substring('left',
1 div (self::AgentName)),
substring('right',
1 div not(self::AgentName)))"/>
<td style="text-align:{$vAlign};">
<span style="font:14px arial; font-weight:bold;text-align:center;">
<xsl:value-of select="."/>
</span>
</td>
</xsl:template>
<xsl:template match="AgentSales/Reason"/>
</xsl:stylesheet>