Using <= and >= in XSLT
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&开发者_如何学运维gt;Average Value</th>
</tr>
<xsl:apply-templates select="//classsize" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="average">
<xsl:choose>
<xsl:when test=". < 1">
<td style="background-color: red;"><xsl:value-of select="." /></td>
</xsl:when>
<xsl:when test="1 <= . < 2">
<td style="background-color: blue;"><xsl:value-of select="." /></td>
</xsl:when>
<xsl:when test="2 <= . < 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
You can escape the <
and >
to <
and >
, 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 <= . < 2
Try:
1 <= . and . < 2
You can't chain the <
and >
like that in XSLT.
In addidtion to @Oded's correct answer:
.1. There is never any need to escape the >
operator in XSLT. Just write: >
.2. One can avoid escaping the <
operator.
Instead of:
x < y
you can write:
not(x >= y)
Instead of:
1 <= . and . < 2
you can write:
2 > . and not(1 > .)
.3. In XPath 1.0 the <
and >
operators are not defined on strings (only on numbers).
Finally, this is actually an XPath 1.0 question.
精彩评论