एक्सएसएलटी <xsl:choose> एलीमेंट

XSLT <xsl:choose> एलीमेंट एक बहुपरिस्थिति परीक्षण व्यक्त करने के लिए <xsl:when> और <xsl:otherwise> को जोड़ने के लिए उपयोग किया जाता है।

<xsl:choose> तत्व

व्याकरण

<xsl:choose>
  <xsl:when test="expression">
    ... आउटपुट ...
  </xsl:when>
  <xsl:otherwise>
    ... आउटपुट ...
  </xsl:otherwise>
</xsl:choose>

चयन कीयोग्यता को कहाँ रखना

XML फ़ाइल के लिए बहुपरिसंकेतन परीक्षण जोड़ने के लिए XSL फ़ाइल में <xsl:choose>、<xsl:when> और <xsl:otherwise> जोड़ें:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

ऊपरी कोड CD की कीमत 10 से अधिक होने पर "Artist" स्तम्भ में गुलाबी पृष्ठभूमि रंग जोड़ेगा。

ऊपर दिए गए परिवर्तन के परिणाम इस तरह हैं:

इस XML फ़ाइल को देखेंइस XSL फ़ाइल को देखेंपरिणाम देखें

एक और उदाहरण

यह एक और दो <xsl:when> तत्वों सहित का उदाहरण है:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:when test="price > 9">
            <td bgcolor="#cccccc">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

ऊपर दिए गए कोड जब CD की कीमत 10 से अधिक हो तो "Artist" स्तम्भ में गुलाबी पृष्ठभूमि रंग जोड़ेगा, और जब CD की कीमत 9 से अधिक और 10 से न्यून तब भी "Artist" स्तम्भ में भूरा पृष्ठभूमि रंग जोड़ेगा।

ऊपर दिए गए परिवर्तन के परिणाम इस तरह हैं:

इस XML फ़ाइल को देखेंइस XSL फ़ाइल को देखेंपरिणाम देखें