跟我学XSL(5)XSL中的choose

来源:互联网 发布:js递归遍历json树 编辑:程序博客网 时间:2024/04/28 08:30
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
上期我们学习了XSL元素<XSL:if>,已能通过测试XML数据的值来决定不同的输出形式,不知你尝试过没有,实际上<XSL:for-each>也可部分实现<XSL:if>的功能,但有时,我们希望对同一数据同时测试多个条件,根据不同条件输出相应结果。当然,我们可以用if,假如我们只有if可用的话。幸好我们有一个更好的选择,那就是用<XSL:choose>。下面介绍相关元素的语法:
  <XSL:choose>
  语法:<XSL:choose>
  属性:无,表示一个多选测试的开始

<XSL:when>
  语法:
<XSL:when expr="script-expression" language="language-name" test="pattern">
  属性:
  expr ── 脚本语言表达式,计算结果为"真"或"假";如果结果为"真",且通过test,则在输出中显示其中内容(可省略此项属性)。
  language ── expr属性中表达式的脚本语言类型,其取值与HTML标记SCRIPT的LANGUAGE属性的取值相同,缺省为"JScript"。
  test ── 源数据测试条件。

<XSL:otherwise>
  语法:<XSL:otherwise>
  属性:无,在一个多选测试中,如果没有不满足<XSL:when>规定的条件,如果在最后有此标记,则输出此标记中的内容。

示例:

此处以学生成绩单为例,要求按成绩的高低给出优秀( >85)、一般(70~85)、及格(60~69)、不及格(< 60),而不是显示分数。其中成绩单的XML文档(文件名:grade.xml)如下:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/XSL" href="grade.XSL"?>
<document>
<grade>
<name>大胖</name>
<english>80</english>
<math>90</math>
<chymest>90</chymest>
</grade>
<grade>
<name>小花</name>
<english>98</english>
<math>70</math>
<chymest>85</chymest>
</grade>
</document>

为实现按分数分等级显示,其XSL文档(文件名:grade.XSL)内容如下:

<?xml version="1.0" encoding="GB2312"?>
<XSL:stylesheet xmlns:XSL="http://www.w3.org/TR/WD-XSL">
<XSL:template match="/">
<HTML>
<HEAD><TITLE>成绩单</TITLE></HEAD>
<BODY>
<XSL:apply-templates select="document"/>
</BODY>
</HTML>
</XSL:template>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击