¿Cómo comprobar qué variable es mayor en xsl?

   <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..

preguntado el 01 de febrero de 12 a las 14:02

Looks fine to me. Neither libxslt nor Transformiix display an error. -

2 Respuestas

Intente lo siguiente:

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

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

Respondido 01 Feb 12, 19:02

No tienes que escapar >. It is absolutely possible to use $a > $b. - Saxofón

You're right, the XSLT parser I used many moons ago got me in the practice of escaping them to avoid parser errors. Just checked now and your code works fine. I learn something new every day, thanks! - Goran Mottram

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 número for the comparison:

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

Respondido 01 Feb 12, 19:02

"When neither object to be compared is a node-set and the operator is <=, <, >= or >, then the objects are compared by converting both objects to numbers and comparing the numbers according to IEEE 754." - No undefined behavior. - Saxofón

@Saxoier: You are right, the paragraph preceding your quote only applied to operators = y !=. - Jörn Horstmann

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.