XSL簡明教程(7)XSL 的控制語句
七. XSL 的控制語句
1.條件語句if...then
XSL同樣還有條件語句(呵呵~~好厲害吧,象程序語言一樣)。具體的語法是增加一個(gè)xsl:if元素,類似這樣
<xsl:if match=".[ARTIST='Bob Dylan']">
... some output ...
</xsl:if>
上面的例子改寫成為:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<xsl:if match=".[ARTIST='Bob Dylan']">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
2. XSL 的Choose
choose的用途是出現(xiàn)多個(gè)條件,給出不同顯示結(jié)果。具體的語法是增加一組xsl:choose,xsl:when,xsl:otherwise元素:
<xsl:choose>
<xsl:when match=".[ARTIST='Bob Dylan']">
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ....
</xsl:otherwise>
</xsl:choose>
上面的例子改寫成為:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<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 match=".[ARTIST='Bob Dylan']">
<td bgcolor="#ff0000"><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>
相關(guān)文章
XML基本概念XPath、XSLT與XQuery函數(shù)介紹
這篇文章介紹了XML基本概念之XPath、XSLT與XQuery函數(shù),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05使用XSLT將XML數(shù)據(jù)轉(zhuǎn)換成HTML
使用XSLT將XML數(shù)據(jù)轉(zhuǎn)換成HTML...2006-10-10在 XSL/XSLT 中實(shí)現(xiàn)隨機(jī)排序
在 XSL/XSLT 中實(shí)現(xiàn)隨機(jī)排序...2006-10-10使用純HTML的通用數(shù)據(jù)管理和服務(wù)
使用純HTML的通用數(shù)據(jù)管理和服務(wù)...2006-10-10