user14349884
user14349884

Reputation:

how to do an inequation using XSLT

So i want to do something like that using xsl if the ranking is between 1 and 3 then set the background color to green else if the ranking is between 20 and 23 then set it to red here is my xsl code

            <xsl:attribute name="style">
                    <xsl:if test=" 1 &lt;= ranking &lt;= 3" >
                        <!-- 1<= ranking >=3 -->
                        background-color: LightGreen;
                    </xsl:if>
                    <xsl:if test=" 20 &lt;= ranking &gt;= 23" >
                        <!-- 20<= ranking >=23 -->
                        background-color: red;
                    </xsl:if>
            </xsl:attribute>

my xml file

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="statistiques.xsl" ?> 

<statistical_team>
    <team>
        <team>Real Madrid</team>
        <ranking>1</ranking>
    </team>
    <team>
        <team>Barcelone</team>
        <ranking>8</ranking>
    </team>
    <team>
        <team>Juventus</team>
        <ranking>2</ranking>
    </team>
    <team>
        <team>PSG</team>
        <ranking>5</ranking>
    </team>
    <team>
        <team>Bayern</team>
        <ranking>4</ranking>
    </team>
</statistical_team>

but it doesn't work and also is there a method to make this process dynamic because if we add a 24th team the code will never work

Upvotes: 0

Views: 47

Answers (2)

Sebastien
Sebastien

Reputation: 2714

Can't you just do it simply like this :

<xsl:attribute name="style">
                    <xsl:if test="ranking &lt;= 3" >
                        <!-- 1<= ranking >=3 -->
                        background-color: LightGreen;
                    </xsl:if>
                    <xsl:if test="ranking &gt;= 20" >
                        <!-- 20<= ranking >=23 -->
                        background-color: red;
                    </xsl:if>
            </xsl:attribute>

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163352

The syntax 1 <= rank <= 3 is actually allowed in XPath 1.0, but it doesn't mean what you think - it's sufficiently confusing that it was disallowed in XPath 2.0. You want 1 <= rank and rank <= 3.

The XPath 1.0 meaning is that 1<=rank is evaluated as a boolean, which is then treated as a number (0 or 1) and the resulting number is compared with 3. Since both 0 and 1 are <= 3, the result is always true.

Upvotes: 2

Related Questions