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

XSLT <xsl:when> Element


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

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

<xsl:when> องค์ประกอบที่จะใช้ในการระบุการดำเนินการที่ <xsl:choose> องค์ประกอบ <xsl:when> องค์ประกอบประเมินการแสดงออกและถ้ามันกลับจริงการกระทำที่จะดำเนินการ

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


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

<xsl:when
test="boolean-expression">

  <!-- Content: template -->

</xsl:when>

แอตทริบิวต์

คุณลักษณะ ความคุ้มค่า ลักษณะ
testboolean-expression จำเป็นต้องใช้ ระบุนิพจน์บูลีนได้รับการทดสอบ

ตัวอย่างที่ 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 ธาตุอ้างอิง