Вот классическое решение XSLT :
Заменить
<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>
на :
<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>
Полный код XSLT становится :
<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>
При применении к предоставленному документу XML :
<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>