XSLT <XSL: เลือก> องค์ประกอบ
ความหมายและการใช้งาน
<xsl: เลือก> องค์ประกอบและ <XSL: เมื่อ> และ <XSL ฉะนั้น> ใช้ร่วมเพื่อแสดงการทดสอบหลายเงื่อนไข
ถ้าไม่มี <XSL: เมื่อ> เป็นความจริงกระบวนการ <XSL ฉะนั้น> เนื้อหา
ถ้าไม่มี <XSL: เมื่อ> เป็นความจริงและไม่มี <XSL ฉะนั้น> องค์ประกอบไม่ได้สร้างอะไร
เคล็ดลับ: สำหรับการทดสอบเงื่อนไขง่ายใช้ <XSL: ถ้า> องค์ประกอบแทน
ไวยากรณ์
<xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>
คุณสมบัติ
ไม่
ตัวอย่าง
รหัสต่อไปนี้จะสูงกว่า 10 ในราคาของแผ่นซีดีเพิ่มคอลัมน์สีชมพูศิลปินพื้นหลัง
ตัวอย่าง
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
<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>
ลอง»
ประกาศตัวแปรชื่อ "สี" ของ ค่าของตัวแปรนี้ได้รับมอบหมายให้แอตทริบิวต์สีขององค์ประกอบปัจจุบัน ถ้าองค์ประกอบปัจจุบันมีแอตทริบิวต์ไม่มีสี "สี" เป็นค่าของ "สีเขียว":
<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>
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
คู่มือการใช้งาน XSLT องค์ประกอบอ้างอิง