XSL 的控制语句

摘自:豆豆网

原文链接:http://tech.ddvip.com/2006-04/11440083602921.html

 

1.条件语句if...then

XSL同样还有条件语句(呵呵~~好厉害吧, 象 程序语言一样)。具体的语法是增加一个 xsl:if 元素,类似这样

< xsl: if match=".[ARTIST='Bob Dylan']">

... some output ...

</ xsl:if >

注意:

1.我见过的格式是:< xsl: if test=".[ARTIST='Bob Dylan']"> ,而不是< xsl: if match=".[ARTIST='Bob Dylan']">

2.在""中有一个".",不应漏掉.


上面的例子改写成为:

<?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 >

 

 

posted on 2009-07-01 11:19  王丹小筑  阅读(270)  评论(0)    收藏  举报

导航