Can you put two conditions in an xslt test attribute?

Xslt

Xslt Problem Overview


Is this right for When 4 < 5 and 1 < 2 ?

<xsl:when test="4 &lt; 5 AND 1 &lt; 2" >
<!-- do something -->
</xsl:when>

Xslt Solutions


Solution 1 - Xslt

Not quite, the AND has to be lower-case.

<xsl:when test="4 &lt; 5 and 1 &lt; 2">
<!-- do something -->
</xsl:when>

Solution 2 - Xslt

It does have to be wrapped in an <xsl:choose> since it's a when. And lowercase the "and".

<xsl:choose>
   <xsl:when test="4 &lt; 5 and 1 &lt; 2" >
   <!-- do something -->
   </xsl:when>
   <xsl:otherwise>
   <!-- do something else -->
   </xsl:otherwise>
</xsl:choose>

Solution 3 - Xslt

From XML.com:

> Like xsl:if instructions, xsl:when > elements can have more elaborate > contents between their start- and > end-tags—for example, literal result > elements, xsl:element elements, or > even xsl:if and xsl:choose elements—to > add to the result tree. Their test > expressions can also use all the > tricks and operators that the xsl:if > element's test attribute can use, such > as and, or, and function calls, to > build more complex boolean > expressions.

Solution 4 - Xslt

Maybe this is a no-brainer for the xslt-professional, but for me at beginner/intermediate level, this got me puzzled. I wanted to do exactly the same thing, but I had to test a responsetime value from an xml instead of a plain number. Following this thread, I tried this:

<xsl:when test="responsetime/@value &gt;= 5000 and responsetime/@value &lt;= 8999"> 

which generated an error. This works:

<xsl:when test="number(responsetime/@value) &gt;= 5000 and number(responsetime/@value) &lt;= 8999">

Don't really understand why it doesn't work without number(), though. Could it be that without number() the value is treated as a string and you can't compare numbers with a string?

Anyway, hope this saves someone a lot of searching...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoeView Question on Stackoverflow
Solution 1 - XsltphihagView Answer on Stackoverflow
Solution 2 - XsltAaron PalmerView Answer on Stackoverflow
Solution 3 - XsltHarper ShelbyView Answer on Stackoverflow
Solution 4 - XsltTedView Answer on Stackoverflow