Solved

XSL/XML Change the background-color of a cell based on the value in the cell

Posted on 2004-08-08
5
1,421 Views
Last Modified: 2008-01-09
I want to change the background-color of each cell to light gray if the value in the cell is zero (0)

Please show me an example code

Thanks
0
Comment
Question by:leslynf
  • 3
  • 2
5 Comments
 
LVL 4

Author Comment

by:leslynf
ID: 11745753
Here are the examples for my xml and xsl files

XML:

<dataroot>
<qryDistrict>
<State>AR</State>
<District>Cornell SD</District>
<Value>300</Value>
<Schools>15</Schools>
<Teachers>35</Teachers>
<Students>1,500</Students>
</qryDistrict>
<qryDistrict>
<State>AR</State>
<District>Davis SD</District>
<Value>0</Value>
<Schools>0</Schools>
<Teachers>0</Teachers>
<Students>0</Students>
</qryDistrict>
</dataroot>

XSL:

<xsl:for-each select="$pNodes">
      <xsl:sort select="*[local-name() = $pSortByState]" data-type="text" order="ascending"/>
      <xsl:sort select="*[local-name() = $pSortBy]" data-type="{$pType}" order="{$pDirection}"/>
      <TR>
           <TD ALIGN="left">
            <A href="AEPNonPartDistricts.html?state={State}"><xsl:value-of select="State"/></A>
           </TD>
           <TD ALIGN="left">
            <A href="AEPbySchool.html?district={District}&amp;state={State}"><xsl:value-of select="District"/></A>
           </TD>
           <TD ALIGN="right">$<xsl:value-of select="format-number(Value, '###,###,###,##0')"/></TD>
           <TD ALIGN="right"><xsl:value-of select="format-number(Schools , '###,###,###,##0')"/></TD>
           <TD ALIGN="right"><xsl:value-of select="format-number(Teachers , '###,###,###,##0')"/></TD>
           <TD ALIGN="right"><xsl:value-of select="format-number(Students , '###,###,###,##0')"/></TD>
      </TR>
</xsl:for-each>
0
 
LVL 5

Accepted Solution

by:
conorj earned 500 total points
ID: 11745887
Assuming you haven't already declared an attribute style for the td element then you could try the following:

...........
<xsl:template match="td">
    <td>
        <xsl:if test=". = 0">
            <xsl:attribute name="style">background-color: #C0C0C0;</xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="@*|*" />
    </td>
</xsl:template>
.............

rgds,
Conor.
0
 
LVL 4

Author Comment

by:leslynf
ID: 11746198
Exactly where am I placing this code? I tried it in several different places -- no luck!

Leslyn :)
0
 
LVL 5

Expert Comment

by:conorj
ID: 11750643
Leslyn,

Where exactly have you tried it??

rgds,
Conor.
0
 
LVL 4

Author Comment

by:leslynf
ID: 11755851
Thanks for your info Conor; however, I resolved it in a different manner.

For other's information, the solution I used is as follows:

<TD ALIGN="right">
    <xsl:if test="Value = 0">
        <xsl:attribute name="style">background-color:#EEEEEE;</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="format-number(Value , '###,###,###,##0')"/>
</TD>
<TD ALIGN="right">
    <xsl:if test="Schools = 0">
        <xsl:attribute name="style">background-color:#EEEEEE;</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="format-number(Schools , '###,###,###,##0')"/>
</TD>
<TD ALIGN="right">
    <xsl:if test="Teachers = 0">
        <xsl:attribute name="style">background-color:#EEEEEE;</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="format-number(Teachers , '###,###,###,##0')"/>
</TD>
<TD ALIGN="right">
    <xsl:if test="Students = 0">
        <xsl:attribute name="style">background-color:#EEEEEE;</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="format-number(Students , '###,###,###,##0')"/>
</TD>
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c#, split 12 80
xml attribute DTD attribute differences 3 65
XML & .net 5 41
URGENT: Need to Import XML file into ACCESS 2013 without using Application.ImportXML function 6 25
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now