<xsl:choose>
標記指定針對節點內容的多個條件測試以及<xsl:otherwise>
和<xsl:when>
元素。
宣告
以下是<xsl:choose>
元素的語法宣告。
<xsl:choose >
</xsl:choose>
元素
xsl:attribute
, xsl:comment
, xsl:copy
, xsl:element
, xsl:fallback
, xsl:for-each
, xsl:if
, xsl:message
, xsl:otherwise
, xsl:param
, xsl:processing-instruction
, xsl:template
, xsl:variable
, xsl:when
, xsl:with-param
, 輸出元素xsl:otherwise
, xsl:when
此範例通過疊代每個學生建立一個<student>
元素的表格,其元素屬性為rollno
,其子元素:<firstname>
,<lastname>
,<nickname>
和<marks>
。 它會檢查並列印等級詳細資訊。
檔案:students.xml -
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
<student rollno = "10010">
<firstname>Machine</firstname>
<lastname>Lee</lastname>
<nickname>Machine</nickname>
<marks>85</marks>
</student>
<student rollno = "10020">
<firstname>Max</firstname>
<lastname>Su</lastname>
<nickname>Maxsu</nickname>
<marks>95</marks>
</student>
<student rollno = "10030">
<firstname>Yii</firstname>
<lastname>Bai</lastname>
<nickname>Yiibai</nickname>
<marks>90</marks>
</student>
</class>
檔案:students.xsl -
<?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>學生資訊列表</h2>
<table border = "1">
<tr bgcolor = "#3498db">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
<th>Grade</th>
</tr>
<xsl:for-each select = "class/student">
<tr>
<td><xsl:value-of select = "@rollno"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "marks"/></td>
<td>
<xsl:choose>
<xsl:when test = "marks > 90">
High
</xsl:when>
<xsl:when test = "marks > 85">
Medium
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
在瀏覽器中打上面檔案:student.xml,效果如下所示 -