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

XSLT <xsl:otherwise> Element


<สมบูรณ์ XSLT ธาตุอ้างอิง

ความหมายและการใช้งาน

<xsl:otherwise> องค์ประกอบระบุการดำเนินการเริ่มต้นสำหรับ <xsl:choose> องค์ประกอบ การกระทำนี้จะเกิดขึ้นเมื่อไม่มี <xsl:when> เงื่อนไข


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

<xsl:otherwise>

  <!-- Content:template -->

</xsl:otherwise>

แอตทริบิวต์

ไม่มี

ตัวอย่างที่ 1

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

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

ดูไฟล์ XML , ดูไฟล์ XSL และ ดูผลที่ได้

ตัวอย่างที่ 2

ประกาศตัวแปรชื่อ "color" ตั้งความคุ้มค่ากับ color แอตทริบิวต์ขององค์ประกอบปัจจุบัน ถ้าองค์ประกอบปัจจุบันมีแอตทริบิวต์ไม่มีสีค่าของ "color" จะเป็น "green" :

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

<สมบูรณ์ XSLT ธาตุอ้างอิง