我正在尝试设置每个其他<TR>
的背景颜色我没有使用for-each循环,因为我使用一些模板根据节点值过滤数据。
<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>
<xsl:sort select="time" data-type="number" order="descending"/>
</xsl:apply-templates>
</table>
<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"/>
这是我的XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
答案 0 :(得分:2)
您可以使用:nth-child([even/odd])
:
tr:nth-child(even) td {
background-color: #fff;
}
tr:nth-child(odd) td {
background-color: #ccc;
}
但是,这不适用于旧浏览器。
答案 1 :(得分:1)
以下是经典的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>
答案 2 :(得分:1)
我喜欢CSS风格的解决方案,但是关于这个主题和没有扩展功能的一般问题,你需要使用像Dimitre借用的样式表这样的推送风格:
<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>