xsl简明教程(7)xsl 的控制语句

作者:未知 来源:未知 添加时间:2006年7月3日 字体:

七. 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:&#118alue-of select="title"/></td>

<td><xsl:&#118alue-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:&#118alue-of select="title"/></td>

<xsl:choose>

<xsl:when match=".[artist='bob dylan']">

<td bgcolor="#ff0000"><xsl:&#118alue-of select="artist"/></td>

</xsl:when>

<xsl:otherwise>

<td><xsl:&#118alue-of select="artist"/></td>

</xsl:otherwise>

</xsl:choose>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

ppdesk