ล่าสุดการพัฒนาเว็บบทเรียน
 

XSLT <xsl:choose> Element


<xsl:choose> องค์ประกอบที่จะใช้ร่วมกับ <xsl:when> และ <xsl:otherwise> จะแสดงการทดสอบหลายเงื่อนไข


<xsl:choose> ธาตุ

วากยสัมพันธ์

<xsl:choose>
  <xsl:when test=" ที่จะนำเลือกสภาพ

การแทรกการทดสอบเงื่อนไขหลายกับไฟล์ XML เพิ่ม <XSL: เลือก> <XSL: เมื่อ> และ <xsl:otherwise> องค์ประกอบไปยังแฟ้ม XSL นี้:

ตัวอย่าง

<?xml version="1.0" encoding="UTF-8"?>
<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 &gt; 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>
ลองตัวเอง»

โค้ดข้างต้นจะเพิ่มสีชมพูสีพื้นหลังกับ "Artist" คอลัมน์เมื่อราคาของแผ่นซีดีที่สูงกว่า 10


ตัวอย่างอื่น

นี่คือตัวอย่างที่มีอีกสอง <xsl:when> องค์ประกอบ:

ตัวอย่าง

<?xml version="1.0" encoding="UTF-8"?>
<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 &gt; 10">

          <td bgcolor="#ff00ff">
          <xsl:value-of select="artist"/></td>
        </xsl:when>
        <xsl:when test="price &gt; 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>
ลองตัวเอง»

โค้ดข้างต้นจะเพิ่มสีพื้นหลังสีชมพูกับ "Artist" คอลัมน์เมื่อราคาของแผ่นซีดีที่สูงกว่า 10 และสีเทาสีพื้นหลังเมื่อราคาของแผ่นซีดีจะสูงกว่า 9 และต่ำกว่าหรือเท่ากับ 10