当前位置:首页 > 网络编程 > WEB编程 > XML编程 > XSL简明教程(7)XSL 的控制语句

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

点击次数:31 次 发布日期:2008-11-21 22:27:50 作者:源代码网
源代码网推荐

源代码网整理以下原著: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>

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华