电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> XML专区>>XSL简明教程(7)XSL 的控制语句:

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

来源:远方网络 | 2006-2-28 | (有1822人读过)

七. 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>


XML专区热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号