当前位置: 首页 > 图文教程 > XML家族 > XML > XSL简明教程(7)XSL 的控制语句

XML
WAP教程(10):WML参考手册、WML实例和WML DTD
WAP教程(7):WML 计时器
WAP教程(6):WML 任务
WAP教程(8):WML 变量
WAP教程(2):WAP 基础
WAP教程(1):WAP 简介
XML入门教程:XSLT
XMLHTTPRequest对象
XML入门教程:XPath
XML入门教程:XHTM
XML入门教程:XLink
XML入门教程:分析XM
XML HTML的区别
将XML数据转换成HTML
XML模式:WSDL
XML模式:DocBook XML
XML入门教程:XHTML
XML入门教程:分析XML
XML初学者知道的一些基础知识
XML教程:XPath归纳及总结

XML 中的 XSL简明教程(7)XSL 的控制语句


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 47 ::
收藏到网摘: n/a

原著:Jan Egil Refsnes 翻译:阿捷
七. XSL 的控制语句

1.条件语句if...then

XSL同样还有条件语句(呵呵~~好厉害吧,象程序语言一样)。具体的语法是增加一个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的用途是出现多个条件,给出不同显示结果。具体的语法是增加一组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>