XSLT <xsl:choose> Elemanı

XSLT <xsl:choose> elementi, <xsl:when> ve <xsl:otherwise> ile birleştirerek ç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>

Yukarıdaki 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ı bu gibi görünebilir:

Bu XML dosyasını görüntüle,Bu XSL dosyasını görüntüle,Sonuçları Gör.

Bir diğer örnek

Bu, iki <xsl:when> elementi içeren bir diğer ö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 renk ekler ve CD'nin fiyatı 9'dan yüksek ve 10'a eşit veya düşük olduğunda "Artist" sütununa gri arka renk ekler.

Yukarıdaki dönüşüm sonuçları bu gibi görünebilir:

Bu XML dosyasını gör,Bu XSL dosyasını gör,Sonuçları Gör.