Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using <= and >= in XSLT

Tags:

xslt

xpath

I would like to use <= and >= when comparing values in <xsl:if test="">. How to do that?

Update:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>
    <body>
        <h1>Average classsize per user and module</h1>
        <table border="1">

            <tr>
                <th>User Email</th>
                <th>Module Code</th>
                <th>Average Value</th>
            </tr>
            <xsl:apply-templates select="//classsize" />
        </table>
    </body>
</html>

</xsl:template>

<xsl:template match="average">
    <xsl:choose>
        <xsl:when test=". &lt; 1">
            <td style="background-color: red;"><xsl:value-of select="." /></td>
        </xsl:when>

        <xsl:when test="1 &lt;= . &lt; 2">
            <td style="background-color: blue;"><xsl:value-of select="." /></td>
        </xsl:when>

        <xsl:when test="2 &lt;= . &lt; 3">
            <td style="background-color: yellow;"><xsl:value-of select="." /></td>
        </xsl:when>

        <xsl:otherwise>
            <td style="background-color: white;"><xsl:value-of select="." /></td>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="//classsize">
    <tr>
        <td><xsl:value-of select="email" /></td>
        <td><xsl:value-of select="modulecode" /></td>
        <xsl:apply-templates select="average" />
    </tr>
</xsl:template>

</xsl:stylesheet>

average < 1 - in red
1 <= average < 2 - in blue
2 <= average < 3 - in yellow
average >= 3 - white
like image 809
Moshin Avatar asked Apr 08 '26 08:04

Moshin


1 Answers

You can escape the < and > to &lt; and &gt;, respectively.

See the example for xsl:if on w3schools.


Update:

After seeing you condition, I am not surprised it doesn't work.

Instead of:

1 &lt;= . &lt; 2

Try:

1 &lt;= . and . &lt; 2

You can't chain the < and > like that in XSLT.

like image 56
Oded Avatar answered Apr 10 '26 00:04

Oded