程序员

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

作者:admin 2021-04-23 我要评论

原著:Jan Egil Refsnes 翻译:阿捷 七. XSL 的控制语句 1.条件语句if...then XSL同样还有条件语句(呵呵~~好厉害吧,象程序语言一样)。具体的语法是增加一个xsl:...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
原著: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>


本文转载自网络,原文链接:https://m.jb51.net/article/2351.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 安卓版谷歌 Chrome 浏览器已支持“冻结

    安卓版谷歌 Chrome 浏览器已支持“冻结

  • 免费开源!经典压缩软件7-Zip首次登陆L

    免费开源!经典压缩软件7-Zip首次登陆L

  • Google确认Chrome 89新版:大幅减少内

    Google确认Chrome 89新版:大幅减少内

  • 鸿蒙第三方组件*SwipeCaptcha滑动拼图

    鸿蒙第三方组件*SwipeCaptcha滑动拼图

腾讯云代理商
精彩导读
海外云服务器
热门资讯
腾讯云代理商