XSLT <xsl:choose> Elemanı

XSLT <xsl:choose> öğesi, <xsl:when> ve <xsl:otherwise> ile çoklu koşul testi ifade etmek için kullanılır.

<xsl:choose> elementi

gramer

<xsl:choose>
  <xsl:when test="ifadesi">
    ... Çıktı ...
  </xsl:when>
  <xsl:otherwise>
    ... Çıktı ...
  </xsl:otherwise>
</xsl:choose>

Seçim koşullarını nerede yerleştirmek

XML dosyasına çoklu koşul testi eklemek için <xsl:choose>, <xsl:when> ve <xsl:otherwise> ekleyin:

<?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>

Üsteki kod, CD'nin fiyatı 10'dan yüksek olduğunda "Artist" sütununa pembe arka plan rengi ekler.

Yukarıdaki dönüşüm sonuçları şu şekilde benzer:

Bu XML dosyasını kontrol edinBu XSL dosyasını kontrol edinSonuçları Gör

Diğer bir örnek

Bu, iki <xsl:when> elementi içeren başka bir örnek:

<?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>

Yukarıdaki kod, CD'nin fiyatı 10'dan yüksek olduğunda "Artist" sütununa pembe arka plan rengi ekler ve CD'nin fiyatı 9'dan yüksek olup 10'a eşit veya düşük olduğunda "Artist" sütununa gri arka plan rengi ekler.

Yukarıdaki dönüşüm sonuçları şu şekilde benzer:

Bu XML dosyasını görBu XSL dosyasını görSonuçları Gör