How check which variable is greater in xsl?

2019-06-20 09:04发布

问题:

   <xsl:variable name="a">20</xsl:variable>
    <xsl:variable name="b">10</xsl:variable>

      <xsl:if test="($a) > ($b)">
        ------
  </xsl:if>

I getting error in the if condion..

回答1:

Try the following :

 <xsl:if test="$a &gt; $b">

Try using the character entities for > (&gt;) and < (&lt;) operators in expressions, otherwise some parsers think you are closing the tag early, or opening another.



回答2:

The example you posted should work. However, you should not that in your case both variables are of type string which could give surprising results where their length differs. The behaviour of the comparison operator on different datatypes is specified in the xpath spec on booleans.

To avoid this you could declare the variables using the select attribute or manually convert them to number for the comparison:

<xsl:variable name="a" select="20"/>
<xsl:variable name="b" select="10"/>
...
<xsl:if test="number($a) > number($b)">
</xsl:if>


标签: xslt