riteshtch
riteshtch

Reputation: 8769

"choose when" with variables XSLT

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 &ge; 75.0">green</xsl:when>
<xsl:when test="$PercentageValue &lt; 75.0 and $PercentageValue &ge; 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 &gt;= 75.0">green</xsl:when>
            <xsl:when test="$PercentageValue &lt; 75.0 and $PercentageValue &gt;= 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 Without using any bgcolor attribute for tr 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

Answers (1)

StuartLC
StuartLC

Reputation: 107267

There are a couple of issues:

  • Your variable is assigned as percent but you reference $PercentageValue
  • Convert a number using the number() function
  • And use gt;= and lt; for comparisons

Try 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 &gt;= 75.0">green</xsl:when>
            <xsl:when test="$PercentageValue &lt; 75.0 and $PercentageValue &gt;= 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() &gt;= 75]">
        <color>green</color>
    </xsl:template>

    <xsl:template match="/xml/PercentageValue[number() &lt; 75.0 and number() &gt;= 50.0]">
        <color>orange</color>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 6

Related Questions