【问题标题】:Alternate Row Color of Table using XSLT使用 XSLT 的表格的替代行颜色
【发布时间】:2011-11-16 18:42:51
【问题描述】:

我有一个 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,但这并不能真正解决问题。有没有办法避免这种障碍?

【问题讨论】:

    标签: xslt


    【解决方案1】:

    试试下面的代码:

    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>

    【讨论】:

    • 请为您的答案添加一些解释。
    【解决方案2】:

    最简单的解决方案(将您的示例样式表作为您实际需求的代表)是首先只循环所需的节点。像这样:

    <xsl:template match="/">
        <table>
            <tr>
                <td>Name</td>
                <td>ID</td>
            </tr>
            <xsl:for-each select="//Book[@ID &lt; 10000 and @ID &gt; 6000]">
                <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="@name" /></td>
                <td><xsl:value-of select="@ID" /></td>
            </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
    

    换句话说,不是有条件地在for-each 的正文中包含节点,而是只选择您需要的那些节点。通过这样做,position() 将与您正在迭代的集合相关,并且它将按预期工作。

    例如,这个(简化的)输入:

    <r>
        <Book ID="6200"/>
        <Book ID="7100"/>
        <Book/>
        <Book/>
        <Book ID="8000"/>
        <Book/>
        <Book ID="9001"/>
        <Book ID="9002"/>
    </r>
    

    产生正确的交替:

    <table>
        <tr>
            <td>Name</td>
            <td>ID</td>
        </tr>
        <tr bgcolor="#D3DFEE">
            <td />
            <td>6200</td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td />
            <td>7100</td>
        </tr>
        <tr bgcolor="#D3DFEE">
            <td />
            <td>8000</td>
        </tr>
        <tr bgcolor="#FFFFFF">
            <td />
            <td>9001</td>
        </tr>
        <tr bgcolor="#D3DFEE">
            <td />
            <td>9002</td>
        </tr>
    </table>
    

    【讨论】:

    • 对于我尝试创建的给定简单示例,这是一个很好的答案,因此可以接受。然而,我的实际 XSLT 要复杂得多,所以我不能完全应用它(或者如果我这样做了,那将是一个复杂的修复,我不值得仅仅为了演示目的)。
    【解决方案3】:

    一个简单的解决方案是记住最后使用的颜色,而不是使用相反的颜色 而是将您的选择基于 position() 并且您应该将选择移到行的测试中

    这是伪代码

    currentColor = color1
    for each 
    if ( id > 10000 and id < 6000 ) {
      if ( currentColor == color1 )
          currentColor= color2
      else 
          currentColor = color1
      showDataInColor(currentColor)
    }
    

    【讨论】:

    • 这种方法不适用于 XSLT,因为变量是不可变的
    猜你喜欢
    • 2013-11-18
    • 2016-08-02
    • 1970-01-01
    • 2014-11-09
    • 2011-03-06
    • 1970-01-01
    • 2023-04-03
    • 2011-12-19
    • 2020-11-12
    相关资源
    最近更新 更多