XSL简明教程(7)XSL 的控制语句
2008-07-28 21:10
281 查看
原著: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>
七. 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>
相关文章推荐
- XSL简明教程(7)XSL 的控制语句
- XSL简明教程(7)XSL 的控制语句
- XSL简明教程(7)XSL 的控制语句
- XSL简明教程(7)XSL 的控制语句
- XSL简明教程(5)XSL的索引
- C#教程第四课:循环控制语句
- linux之SQL语句简明教程---UNION
- c#教程第四课循环控制语句
- linux之SQL语句简明教程---IN
- XSL 简明教程 (下)
- XSL简明教程(5)XSL的索引
- Octave教程五:控制语句if/while/for以及函数的定义和使用
- Shell脚本case语句简明教程
- XSL简明教程(2)XSL转换
- linux之SQL语句简明教程
- linux之SQL语句简明教程---SELECT
- linux之SQL语句简明教程---AND OR
- linux之SQL语句简明教程---LIKE
- linux之SQL语句简明教程---Alias
- linux之SQL语句简明教程---UNION ALL