gpt4 book ai didi

xslt - 使用 XSLT 的表的替代行颜色

转载 作者:行者123 更新时间:2023-12-02 00:26:02 25 4
gpt4 key购买 nike

我有一个 XSLT,我想在其中替换输出表的行颜色。我知道您可以使用本示例中的代码:

<table>
<tr>
<td>Name</td>
<td>ID</td>
</tr>
<xsl:for-each select="//Book">
<xsl:variable name="altColor">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">#FFFFFF</xsl:when>
<xsl:otherwise>#D3DFEE</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$altColor}">
<td><xsl:value-of select="current()/@name"/></td>
<td><xsl:value-of select="current()/@ID"/></td>
</tr>
</xsl:for-each>
</table>

工作正常,但是,我有一些情况需要在 for-each 中包含一些 if 语句,例如。

<table>
<tr>
<td>Name</td>
<td>ID</td>
</tr>
<xsl:for-each select="//Book">
<xsl:variable name="altColor">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">#FFFFFF</xsl:when>
<xsl:otherwise>#D3DFEE</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="current()/@ID &gt; 10000 and current/@ID &lt; 6000">
<tr bgcolor="{$altColor}">
<td><xsl:value-of select="current()/@name"/></td>
<td><xsl:value-of select="current()/@ID"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>

然后它不起作用,因为它可能会跳过 for-each 中某个位置的项目,并且我最终得到随机交替的行颜色,或者它可能从错误的位置开始,其中行以错误的颜色开始交替。

我尝试添加 xsl:sort,但这并不能真正解决问题。有没有办法避免这个障碍?

最佳答案

尝试使用以下代码:

tr[position() mod 2 =0]

<xsl:template match="n1:tr[position() mod 2 =0]">

<tr bgcolor="#aaaaaa">

<xsl:apply-templates/>

</tr>
</xsl:template>



<xsl:template match="n1:tr[position() mod 2 =1]">

<tr bgcolor="#aaaaff">

<xsl:apply-templates/>

</tr>

</xsl:template>

关于xslt - 使用 XSLT 的表的替代行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8156906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com