XSL简明教程(7)XSL 的控制

七. XSL 的控制语句

1.条件语句if...then

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

1<xsl:if dylan\']\"="" match="\&quot;.[ARTIST=\'Bob">   
2  
3... some output ...   
4  
5</xsl:if>

上面的例子改写成为:

 1<xsl:stylesheet xmlns:xsl='\"http://www.w3.org/TR/WD-xsl\"'>
 2<xsl:template match='\"/\"'>
 3<html>
 4<body>
 5<table bgcolor='\"yellow\"' border='\"2\"'>
 6<tr>
 7<th>Title</th>
 8<th>Artist</th>
 9</tr>
10<xsl:for-each select='\"CATALOG/CD\"'>
11<xsl:if dylan\']\"="" match="\&quot;.[ARTIST=\'Bob">
12<tr>
13<td><xsl:value-of select='\"TITLE\"/'></xsl:value-of></td>
14<td><xsl:value-of select='\"ARTIST\"/'></xsl:value-of></td>
15</tr>
16</xsl:if>
17</xsl:for-each>
18</table>
19</body>
20</html>
21</xsl:template>
22</xsl:stylesheet>

2. XSL 的Choose

choose的用途是出现多个条件,给出不同显示结果。具体的语法是增加一组xsl:choose,xsl:when,xsl:otherwise元素:

 1<xsl:choose>
 2<xsl:when dylan\']\"="" match="\&quot;.[ARTIST=\'Bob">   
 3  
 4... some code ...   
 5  
 6</xsl:when>
 7<xsl:otherwise>   
 8  
 9... some code ....   
10  
11</xsl:otherwise>
12</xsl:choose>

上面的例子改写成为:

 1<xsl:stylesheet xmlns:xsl='\"http://www.w3.org/TR/WD-xsl\"'>
 2<xsl:template match='\"/\"'>
 3<html>
 4<body>
 5<table bgcolor='\"yellow\"' border='\"2\"'>
 6<tr>
 7<th>Title</th>
 8<th>Artist</th>
 9</tr>
10<xsl:for-each select='\"CATALOG/CD\"'>
11<tr>
12<td><xsl:value-of select='\"TITLE\"/'></xsl:value-of></td>
13<xsl:choose>
14<xsl:when dylan\']\"="" match="\&quot;.[ARTIST=\'Bob">
15<td bgcolor='\"#ff0000\"'><xsl:value-of select='\"ARTIST\"/'></xsl:value-of></td>
16</xsl:when>
17<xsl:otherwise>
18<td><xsl:value-of select='\"ARTIST\"/'></xsl:value-of></td>
19</xsl:otherwise>
20</xsl:choose>
21</tr>
22</xsl:for-each>
23</table>
24</body>
25</html>
26</xsl:template>
27</xsl:stylesheet>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus