How to declare xsl:choose element in XSLT

In this article,we are describing xsl:choose element.
  • 1883
The <xsl:choose> element is tests multiple condition in conjunction with the <xsl:otherwise> and <xsl:when> elements.For simple conditional testing, use the <xsl:if> element.It behaves like a switch statement.It is support to IE 5.0 and FF 1.0.

Syntax of xsl:choose element

<xsl:choose>

  <xsl:when test="First Statement"></xsl:when>

  <xsl:when test="Second Statement"></xsl:when>

  <xsl:otherwise></xsl:otherwise> [optional]

</xsl:choose>

  • It have no any attribute.

Example of xsl:chhose element

<?xml version="1.0" encoding="ISO-9000-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.google.com/XSL/Transform"> 

  <xsl:template match="/">

    <html>

      <body>

        <h2>Shrit Price</h2>

        <table border="1">

          <tr bgcolor="red">

            <th>Title</th>

            <th>Title1</th>

          </tr>

          <xsl:for-each select="catalog/shrit">

            <tr>

              <td>

                <xsl:value-of select="title"/>

              </td>

              <xsl:choose>

                <xsl:when test="price &gt; 20">

                  <td bgcolor="blue">

                    <xsl:value-of select="title1"/>

                  </td>

                </xsl:when>

                <xsl:otherwise>

                  <td>

                    <xsl:value-of select="title"/>

                  </td>

                </xsl:otherwise>

              </xsl:choose>

            </tr>

          </xsl:for-each>

        </table>

      </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

Further Readings

You may also want to read these related articles :

Ask Your Question 

Ask Your Question Got a programming related question? You may want to post your question here

Programming Answers here


© 2020 DotNetHeaven. All rights reserved.