Reputation: 8769
I want to set the color of a table row according to the float value ..
....
<xsl:variable name="percent">
<xsl:value-of select="float(PercentageValue)" />
</xsl:variable>
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="$PercentageValue ≥ 75.0">green</xsl:when>
<xsl:when test="$PercentageValue < 75.0 and $PercentageValue ≥ 50.0">orange</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="$color">
....
what i want is ..
if(percent>=75)
tableRowColor=green;
else if(percent>=50.0 && percent <75.0)
tableRowColor=orange;
else
tableRowColor=red;
Im relatively new to XSLT syntax .. what is the problem present above? Help appreciated!
Edit 1: Sorry a typo while copy pasting for the variable being $PercentageValue Here is what ive done now ..
....
<xsl:variable name="PercentageValue">
<xsl:value-of select="number(percent)" />
</xsl:variable>
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="$PercentageValue >= 75.0">green</xsl:when>
<xsl:when test="$PercentageValue < 75.0 and $PercentageValue >= 50.0">orange</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="$color">
.....
Now i do get the colors but all are black ..why so?
Here is how im displaying
Percentage values are at the end .. which is selected in a td for that tr so it does get displayed ..
actually the whole xml and xsl are dynamically generated via java, therefore there is alot of precision(double) over there . .does that cause a problem?
Upvotes: 2
Views: 20082
Reputation: 107267
There are a couple of issues:
percent
but you reference $PercentageValue
number()
function gt;=
and lt;
for comparisonsTry this:
<xsl:template match="/xml">
<xsl:variable name="PercentageValue">
<xsl:value-of select="number(PercentageValue)" />
</xsl:variable>
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="$PercentageValue >= 75.0">green</xsl:when>
<xsl:when test="$PercentageValue < 75.0 and $PercentageValue >= 50.0">orange</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<TheColorIs>
<xsl:value-of select="$color"/>
</TheColorIs>
</xsl:template>
On this Xml:
<xml>
<PercentageValue>77</PercentageValue>
</xml>
As an aside, instead of creating lots of variables and imperatively 'switching' using xsl:if
or xsl:choose / xsl:when
, remember that you can also use template filtering to apply matching:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/xml/PercentageValue[number() >= 75]">
<color>green</color>
</xsl:template>
<xsl:template match="/xml/PercentageValue[number() < 75.0 and number() >= 50.0]">
<color>orange</color>
</xsl:template>
</xsl:stylesheet>
Upvotes: 6