XSLT <xsl:เมื่อ>
❮ ทำการอ้างอิงองค์ประกอบ XSLT ให้สมบูรณ์
ความหมายและการใช้งาน
<xsl:เมื่อ> องค์ประกอบใช้เพื่อระบุการดำเนินการสำหรับ <xsl:choose> องค์ประกอบ <xsl:เมื่อ> องค์ประกอบประเมินนิพจน์ และถ้าส่งคืนจริง การดำเนินการจะดำเนินการ
หมายเหตุ:องค์ประกอบ <xsl:when> ใช้ร่วมกับ <xsl:choose> และ <xsl:otherwise> เพื่อแสดงการทดสอบแบบมีเงื่อนไขหลายรายการ
ไวยากรณ์
<xsl:when
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
คุณลักษณะ
Attribute | Value | Description |
---|---|---|
test | boolean-expression | Required. Specifies a Boolean expression to be tested |
ตัวอย่าง 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 ให้สมบูรณ์