Posts Tagged ‘ XSL:WHEN ’

A Variety of XSL Test Conditions (Collection of XSL:IF and XSL:WHEN TEST Conditions)

Sunday, September 14th, 2008 By Nikhil

Every time I have to do a XSL:IF or XSL:WHEN test, I have go back to the reference books. I never seem to remember them. Thought there would be more out there, just like me, who would rather prefer to have all of them, possible, at one place, ready to be referred when needed.

For those who are new … Syntax for XSL:IF

<xsl:if test=”expression”>
<!– Content: template –>

The xsl:if/xsl:when Test Collection
The conditions below are all simple and self explanatory. So I believe it won’t need much explanation

<xsl:if test=”position()=last()-1″>
This if the last but one element in the list!

<xsl:if test=”SALARY > 5000″>
The salary of this person is greater than Rs. 5000

<xsl:if test=”count(JOBS) > 3″>
There are more than 3 jobs in this list!

<xsl:if test=”(@DEPT = ‘SALES’) or (@JOINYEAR = ’1997′)”>
       This person is from sales department OR his Year of Joining is 1997

<xsl:if test=”EXPERIENCE”>
       This person have atleast one EXPERIENCE child element.

<xsl:if test=”@HOBBY”>
    The person has a hobby attribute.

The same text conditions could be used for XSL:CHOOSE/XSL:WHEN as well


XSLT’s  xsl:choose instruction is similar to xsl:if but has a few key differences:
• One xsl:choose element can test for more than one condition and add different nodes to the result tree based on which condition is true. 
• An xsl:choose element can have a default template to add to the result tree if none of the conditions are true. (Compare xsl:if, which has no equivalent of an “else” condition.)
• The xsl:choose element has specific subelements that are necessary for it to work, while you can put any well-formed elements you want inside of an xsl:if element.

<xsl:when test=”boolean-expression”>
   Do something when this boolean-expression is TURE
<xsl:otherwise> Do something else </xsl:otherwise>

get ExpressingIT News by Email Subscribe to ExpressingIT by Email or  Follow Me on Twitter

 Subscribe to ExpressingIT RSS
get ExpressingIT News by Email Subscribe to ExpressingIT by Email
 Follow Me on Twitter