当前位置: 首页 > 图文教程 > XML家族 > XML > XSL简明教程(7)XSL 的控制语句

XML
一个以Javascript+xml的树型列表
用ajax技术制作在线歌词搜索功能
网络编程:如何生成XML数据
如何让WebServer返回指定XML内容
网页编程必看:XML文法分析
编程:如何生成XML数据
用PHP与XML联手进行网站编程
Javascript+XML实现分页的实例
XML技巧五则
使用xmlhttp为网站增加域名查询功能
详解XML-RPC和JAX-RPC
XML在.net平台下的自定义控件的应用(1)
XML在.net平台下的自定义控件的应用(2)
基础知识认识XML:下一代网络的基石
Web2.0岁月:使用AJAX技术的十大理由
了解 XML实现通用的数据访问
AJAX基础教程及初步使用
Web设计中如何使用XML数据
AJAX应用之草稿自动保存
XML 中的常见问题(一)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 41 ::
收藏到网摘: n/a

原著: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>