·  XSLT – Elements – <xsl:choose> <xsl:when> <xsl:otherwise>

 

- Info :

  - Only content of the first   <xsl:when> that is true will be processed.

  - If no <xsl:when> is true, the content of <xsl:otherwise> is processed.

  - If no <xsl:when> is true, and no <xsl:otherwise> element is present, nothing is created.

 

- Example:

 

    test.xml

    <?xml version="1.0"?>

    <?xml-stylesheet type="text/xsl" href="test.xsl"?>

    

    <people>

      <student id="3">

        <name>Jack  </name>

        <name>Junior</name>

        <age > 23   </age>

      </student>

    </people>

    

    test.xsl

    <?xml version="1.0" ?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    

      <xsl:template match="/people/student">       

        <xsl:choose>

          <xsl:when test="age=23"          >Student has at least 1 age element with value 3</xsl:when>   

          <xsl:when test="name[1]='Junior'">Student's 2nd name element has value 'Junior'  </xsl:when>

          <xsl:when test="@id &lt; 10"     >Student has attribute id less then 10.         </xsl:when>

          <xsl:otherwise                   >No match found.                           </xsl:otherwise>

        </xsl:choose>  

      </xsl:template>

      

    </xsl:stylesheet>