Xslt如何设置条件奇数/偶数行的样式 [英] Xslt how to style conditional odd/even rows

查看:244
本文介绍了Xslt如何设置条件奇数/偶数行的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用xslt转换编写的html表,看起来像这样

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>

我需要一种方法来应用不同的类oddRow / evenRow到tr elems。

I need a way to apply different classes oddRow/evenRow to tr elems.

<tr class="evenRow"> or <tr class="oddRow">

我尝试在每个< tr>之后使用这样的模板elem

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?

推荐答案

你可以在只是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>

请注意,我在SO文本框中写了这个并且没有测试过它。

Note that i wrote this in the SO textbox and haven't tested it.

这篇关于Xslt如何设置条件奇数/偶数行的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆