I need help setting up my HTML output from an XSL file to generate string results in order to have a variable background color based on changing the value of the node element. The data comes from SQL Server 2008, and the query results are sorted by the node element in the "Section Name" section.
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')
The following are all contents of the XSL file:
<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>
I was looking for other questions like this one on your site, but did not find an example where the color is determined when the value of a particular node element changes. Currently, the result is that the table alternates the background color of any other row between #FFFFCC and #CCFFFF, regardless of the value of the section name.
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>
.