广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    XSL最全基础教程之xsl:choose元素

    来源:网络收集  点击:  时间:2024-08-20
    【导读】:
    xsl:choose 元素略有不同,必须与 xsl:when 以及 xsl:otherwise 元素结合,可表达多重条件测试。 如果xsl:when 是 false,则处理 xsl:otherwise 的内容。 如果 xsl:when 是 false,且不存在 xsl:otherwise 元素,则不创建任何内容。工具/原料moreNotepad++XML ToolsFirefox方法/步骤1/6分步阅读

    语法

    xsl:choose

    xsl:when test=expression

    ... 输出 ...

    /xsl:when

    xsl:otherwise .

    ... 输出 ....

    /xsl:otherwise

    /xsl:choose

    其中test属性值的结果必须是true或者false

    2/6

    要点

    将按照从上到下的顺序测试 xsl:choose元素的xsl:when子级,直到其中一个元素上的test属性准确地说明源数据中的条件,或直到遇到xsl:otherwise元素才退出。

    在选择了xsl:when或xsl:otherwise元素之后,将退出xsl:choose块。 不需要明确的中断或退出语句。

    对于简单条件测试,请使用xsl:if元素。

    3/6

    示例

    以下示例显示order元素的模板,并在每个order的内容之前插入一个大小指示器。 该大小指示器基于每个order元素中total元素的值。 如果总计小于 10,则添加文本“(small)”。 如果总计小于 20,则添加文本“(medium)”。 如果总计大于等于 20,则添加文本“(large)”。

    4/6

    创建XML 文件 (order.xml)

    ?xml version=1.0?

    ?xml-stylesheet type=text/xsl href=refchoose.xsl ?

    orders

    order

    lineitem/

    lineitem/

    total8/total

    /order

    order

    lineitem/

    lineitem/

    total18/total

    /order

    order

    lineitem/

    lineitem/

    total28/total

    /order

    /orders

    5/6

    XSLT 文件 (refchoose.xsl)

    ?xml version=1.0?

    xsl:stylesheet version=1.0

    xmlns:xsl=http://www.w3.org/1999/XSL/Transform

    xsl:template match=order

    xsl:choose

    xsl:when test=total lt; 10

    (small)

    /xsl:when

    xsl:when test=total lt; 20

    (medium)

    /xsl:when

    xsl:otherwise

    (large)

    /xsl:otherwise

    /xsl:choose

    xsl:apply-templates /

    BR/

    /xsl:template

    /xsl:stylesheet

    6/6

    运行结果

    用Firefox打开order.xml文件给出结果

    (small) 9

    (medium) 19

    (large) 29

    示例21/1

    创建xsl文件

    ?xml version=1.0 encoding=ISO-8859-1?

    xsl:stylesheet version=1.0

    xmlns:xsl=http://www.w3.org/1999/XSL/Transform

    xsl:template match=/

    html

    body

    h2My CD Collection/h2

    table border=1

    tr bgcolor=#9acd32

    thTitle/th

    thArtist/th

    /tr

    xsl:for-each select=catalog/cd

    tr

    tdxsl:value-of select=title//td

    xsl:choose

    xsl:when test=price gt; 10

    td bgcolor=#ff00ff

    xsl:value-of select=artist//td

    /xsl:when

    xsl:otherwise

    tdxsl:value-of select=artist//td

    /xsl:otherwise

    /xsl:choose

    /tr

    /xsl:for-each

    /table

    /body

    /html

    /xsl:template

    /xsl:stylesheet

    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_1143439.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-07 04:56:18  耗时:0.028
    0.0283s