5

我需要帮助来设置来自 XSL 文件的 HTML 输出,以根据节点元素的值何时更改来生成具有交替背景颜色的行结果。数据来自 SQL Server 2008,查询结果按“DivisionName”的节点元素排序。

SELECT      
  d.DivisionName,
  CASE
        WHEN t.ActualFinish is not NULL
              THEN convert(varchar, datepart(mm, t.ActualFinish))+ '/' + convert(varchar, datepart(dd, t.ActualFinish)) + '/' + convert(varchar, datepart(yyyy, t.ActualFinish))
        ELSE ' '
  END AS "Date",
  r.RegionName, 
  t.Name, 
  CASE
        WHEN t.ScheduleByElement is not NULL
              THEN t.ScheduleByElement
        ELSE ' '
  END AS "ScheduleByElement",
  '' AS "Activity",
  '' AS "Team",
  CASE
        WHEN t.WatchStatus is not NULL
              THEN convert(varchar, datepart(mm, t.WatchStatus))+ '/' + convert(varchar, datepart(dd, t.WatchStatus)) + '/' + convert(varchar, datepart(yyyy, t.WatchStatus))
        ELSE ' '
  END AS "Status"
FROM           
  dbo.Tasks AS t 
        INNER JOIN dbo.Division AS d 
              ON t.fk_DivisionID = d.DivisionID 
        INNER JOIN dbo.Region AS r 
              ON t.fk_RegionID = r.RegionID
WHERE        
  (t.OutlineLevel = 3) 
  AND (t.ScheduleByElement <> '') 
  AND (t.FileName LIKE 'NVDE%')
  AND (t.ActualFinish is not NULL)
ORDER BY d.DivisionName, t.ActualFinish, r.RegionName 
FOR XML PATH('DivisonName'), ROOT('CCDN')

下面列出了 XSL 文件的完整内容:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/ROOT">


    <table border="1" cellpadding="6">
        <tr bgcolor="#9acd32">
            <td>Division</td>
            <td>Date</td>
            <td>Region</td>
            <td>System Name</td>
            <td>Schedule by/Plan by Element</td>
            <td>Activity</td>
            <td>Deployment Engineering Team</td>
            <td>Status</td>
        </tr>

        <xsl:for-each select="CCDN/DivisonName [not(preceding-sibling::DivisionName[1]/@DivisionNameID = @DivisionNameID)]">
            <xsl:variable name="DivisionNameID" select="@DivisionNameID" />
            <xsl:variable name="bgcolor">
                <xsl:choose>
                    <xsl:when test="position() mod 2 = 1">#FFFFCC</xsl:when>
                    <xsl:otherwise>#CCFFFF</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <xsl:for-each select=".|following-sibling::DivisionName[@DivisionNameID = $DivisionNameID]">
                <tr bgcolor="{$bgcolor}">
                    <xsl:for-each select="./*">
                        <td>
                            <xsl:value-of select="." />
                        </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </xsl:for-each>

    </table>


</xsl:template>

我在您的网站上搜索了与此类似的其他问题,但没有找到在特定节点元素的变化值上定义颜色的示例。目前,结果是表格在#FFFFCC 和#CCFFFF 之间交替每隔一行的背景颜色,而不管 DivisionName 的值。

以下 HTML 可能更好地说明了我正在寻找的结果。行根据分区 A、B、C 和 D 升序排序。与第一个分区相关的所有行都是一种阴影(白色)或颜色,与下一个分区相关的所有行都是另一种阴影或颜色,然后从那时起重复。如果没有为 A 返回行,则 B 将是白色和交替的。没有为特定除法值定义特定阴影或颜色的硬编码。无论返回的除法如何,它都会改变,并且只关心除法值是否发生变化。

<style type="text/css">
        table.mystyle
        {
        border-width: 0 0 1px 1px;
        border-spacing: 0;
        border-collapse: collapse;
        border-style: solid;
        }
        .mystyle td, .mystyle th
        {
        margin: 0;
        padding: 4px;
        border-width: 1px 1px 0 0;
        border-style: solid;
        }
        tr.oddcolor
        {
        Background-color:#FFFFFF;
        }
        tr.evencolor
        {
        Background-color:#CCCCCC;
        }
    </style>
<table class="mystyle">
<tr>
<b>
  <td>Division</td>
  <td>Date</td>
  <td>Region</td>
  <td>System Name</td>
  <td>Schedule by/Plan by Element</td>
  <td>Activity</td>
  <td>Deployment Engineering Team</td>
  <td>Status</td>
</b>
 </tr>
 <tr bgcolor="#FFFFCC">
  <td>A</td>
  <td>3/2/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#FFFFCC">
  <td>A</td>
  <td>4/12/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#FFFFCC">
  <td>A</td>
  <td>4/12/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#CCFFFF">
  <td>B</td>
  <td>4/27/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#CCFFFF">
  <td>B</td>
  <td>5/1/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#FFFFCC">
  <td>C</td>
  <td>5/15/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#FFFFCC">
  <td>C</td>
  <td>5/24/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr bgcolor="#CCFFFF">
  <td>D</td>
  <td>5/25/2012</td>
  <td>region</td>
  <td>xxx</td>
  <td>xxxx</td>
  <td></td>
  <td></td>
  <td></td>
</tr>
</table>

谢谢。

4

1 回答 1

1

那我再来一次狂欢。此方法依赖于已按部门排序的数据。

XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <row division="a">Data</row>
    <row division="a">D1123ta</row>
    <row division="b">Da123ta</row>
    <row division="b">Da14ta</row>
    <row division="b">Da12312ta</row>
    <row division="c">Da123ta</row>
    <row division="c">Dat123a</row>
    <row division="c">Da123ta</row>
    <row division="c">Dat123a</row>
    <row division="d">Da123ta</row>
    <row division="d">Dat123a</row>
</root>

XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:key name="arb" match="/root/row" use="@division"/>
    <xsl:template match="/">
       <!-- <table>
            <xsl:for-each select="root/row">
                <xsl:if test="generate-id(.) = generate-id(key('arb',@division))">
                    <tr><td>New Colour</td></tr>
                </xsl:if>
                <tr><td><xsl:value-of select="."/></td></tr>
            </xsl:for-each>
        </table>-->
        <html>
            <body>
                <table>
            <xsl:for-each select="root/row[generate-id(.) = generate-id(key('arb',@division))]">
                <xsl:variable name="local_div" select="@division"/>
                <tbody>
                    <xsl:if test="position() mod 2 = 0">
                        <xsl:attribute name="bgcolor">red</xsl:attribute>
                    </xsl:if>
                    <xsl:for-each select="../row[@division=$local_div]">
                        <tr><td><xsl:value-of select="@division"/></td><td><xsl:value-of select="."/></td></tr>
                    </xsl:for-each>
                </tbody>
            </xsl:for-each>
        </table>
            </body>
        </html>

    </xsl:template>
</xsl:stylesheet>

产品

<html>
<body>
  <table>
     <tbody>
        <tr>
           <td>a</td>
           <td>Data</td>
        </tr>
        <tr>
           <td>a</td>
           <td>D1123ta</td>
        </tr>
     </tbody>
     <tbody bgcolor="red">
        <tr>
           <td>b</td>
           <td>Da123ta</td>
        </tr>
        <tr>
           <td>b</td>
           <td>Da14ta</td>
        </tr>
        <tr>
           <td>b</td>
           <td>Da12312ta</td>
        </tr>
     </tbody>
     <tbody>
        <tr>
           <td>c</td>
           <td>Da123ta</td>
        </tr>
        <tr>
           <td>c</td>
           <td>Dat123a</td>
        </tr>
        <tr>
           <td>c</td>
           <td>Da123ta</td>
        </tr>
        <tr>
           <td>c</td>
           <td>Dat123a</td>
        </tr>
     </tbody>
     <tbody bgcolor="red">
        <tr>
           <td>d</td>
           <td>Da123ta</td>
        </tr>
        <tr>
           <td>d</td>
           <td>Dat123a</td>
        </tr>
     </tbody>
  </table>
</body>
</html>
于 2012-09-19T12:44:31.297 回答