विश्व का सबसे बड़ा हिंदी वेब डेवलपर साइट

XSLT <xsl:choose> Element

XSLT Element Reference XSLT Element Reference

Definition and Usage

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.

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.

Tip: For simple conditional testing, use the <xsl:if> element instead.



  <!-- Content:(xsl:when+,xsl:otherwise?) -->





The code below will add a pink background-color to the artist column WHEN the price of the CD is higher than 10.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"

<xsl:template match="/">
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
      <xsl:for-each select="catalog/cd">
        <td><xsl:value-of select="title"/></td>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
            <td><xsl:value-of select="artist"/></td>

Try it yourself »

Declare a variable named "color". Set its value to the color attribute of the current element. If the current element has no color attribute, the value of "color" will be "green":

<xsl:variable name="color">
    <xsl:when test="@color">
      <xsl:value-of select="@color"/>

XSLT Element Reference XSLT Element Reference