XSLT <xsl:otherwise>
❮ ทำการอ้างอิงองค์ประกอบ XSLT ให้สมบูรณ์
ความหมายและการใช้งาน
องค์ประกอบ <xsl:otherwise> ระบุการดำเนินการเริ่มต้นสำหรับองค์ประกอบ <xsl:choose> การดำเนินการนี้จะเกิดขึ้นเมื่อไม่มีเงื่อนไข <xsl:when> บังคับใช้
ไวยากรณ์
<xsl:otherwise>
<!-- Content:template -->
</xsl:otherwise>
คุณลักษณะ
ไม่มี
ตัวอย่าง 1
รหัสด้านล่างจะเพิ่มสีพื้นหลังสีชมพูให้กับคอลัมน์ศิลปิน เมื่อราคาของ cd สูงกว่า 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>'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
ประกาศตัวแปรชื่อ "สี" ตั้งค่าเป็นแอตทริบิวต์สีขององค์ประกอบปัจจุบัน หากองค์ประกอบปัจจุบันไม่มีแอตทริบิวต์สี ค่าของ "สี" จะเป็น "สีเขียว":
<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 ให้สมบูรณ์