choose/when/otherwise:首个命中与完整边界
保留一个when和两个when示例,并真正核对颜色及默认分支。
学完这一章,你能够
- 保留一个when和两个when示例,并真正核对颜色及默认分支。
- 保留一个when和两个when示例,并真正核对颜色及默认分支。
按顺序只选一个
choose内按顺序检查when,执行首个成立分支;otherwise可选且放末尾。它不是让所有真条件都运行,条件次序会改变结果。
本章价格9、10、11覆盖>10粉色、>9灰色、其它默认;另保留只有一个when的粉色/默认两路。等于10落灰,等于9落默认。
语义与呈现对应
现代例用class与CSS表达颜色,源知识中的bgcolor作为历史属性说明保留;必须检查实际计算背景而非只有class文字。
缺失或非数字price另说明NaN影响,不让异常被误算成正常价格。
语法、教学目标与适用条件
| 项目 | 写法/条件 | 用途与结果 | 限制/版本 |
| 第一when | price >10 | 11艺人洋红底 | 首个成立即终止选择 |
| 第二when | price >9 | 10艺人灰底 | 仅双条件实例 |
| otherwise | 其它输入 | 9艺人白底 | 单条件时10也在此分支 |
本章全部转换分支与源码对应
| 项目 | 写法/条件 | 用途与结果 | 限制/版本 |
| 1 | styles/choose-1.xsl | 1个when和默认边界 | 完整预期在本章checks逐项断言 |
| 2 | styles/choose-2.xsl | 2个when和默认边界 | 完整预期在本章checks逐项断言 |
本章独立核心源码与验证步骤
展开查看收起
python
from xslt_lab import run_cases
# 蓝色天空网 mbsky.com:本章完整输入、规则与可观察预期。
SCENARIOS = [{'id': '1',
'style': 'styles/choose-1.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': '//table/tr[position() > 1]/td[2]/@class',
'expected': ['high', 'ordinary', 'ordinary']}],
'goal': '1个when和默认边界'},
{'id': '2',
'style': 'styles/choose-2.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': '//table/tr[position() > 1]/td[2]/@class',
'expected': ['high', 'middle', 'ordinary']}],
'goal': '2个when和默认边界'}]
if __name__=="__main__":
run_cases('xsl-choose',SCENARIOS)
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 本章2个分支的实际结果应与各自checks一致,故意错误必须拒绝。
- 具体目标:1个when和默认边界;2个when和默认边界
完整样式与指定输入:choose-1.xsl
展开查看收起
xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method="html" encoding="UTF-8" doctype-system="about:legacy-compat"/><xsl:template match="/"><html lang="zh-CN"><head><meta charset="UTF-8"/><meta name="robots" content="noindex,nofollow"/><title>蓝色天空网 · XSLT独立结果</title><style>body{max-width:1080px;margin:32px auto;padding:24px;color:#182d49;font:18px/1.8 sans-serif}table{border-collapse:collapse;width:100%}td,th{border:1px solid #bdd0e7;padding:12px}.high{background:#ff00ff}.middle{background:#cccccc}.ordinary{background:white}.title{color:#ff0000}.artist{color:#008000}</style></head><body><h1>蓝色天空网 · mbsky.com</h1><table><tr><th>标题</th><th>艺人</th><th>价格</th></tr><xsl:for-each select="catalog/cd"><tr><td><xsl:value-of select="title"/></td><xsl:choose><xsl:when test="price > 10"><td class="high"><xsl:value-of select="artist"/></td></xsl:when><xsl:otherwise><td class="ordinary"><xsl:value-of select="artist"/></td></xsl:otherwise></xsl:choose><td><xsl:value-of select="price"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 1个when和默认边界
完整样式与指定输入:choose-2.xsl
展开查看收起
xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method="html" encoding="UTF-8" doctype-system="about:legacy-compat"/><xsl:template match="/"><html lang="zh-CN"><head><meta charset="UTF-8"/><meta name="robots" content="noindex,nofollow"/><title>蓝色天空网 · XSLT独立结果</title><style>body{max-width:1080px;margin:32px auto;padding:24px;color:#182d49;font:18px/1.8 sans-serif}table{border-collapse:collapse;width:100%}td,th{border:1px solid #bdd0e7;padding:12px}.high{background:#ff00ff}.middle{background:#cccccc}.ordinary{background:white}.title{color:#ff0000}.artist{color:#008000}</style></head><body><h1>蓝色天空网 · mbsky.com</h1><table><tr><th>标题</th><th>艺人</th><th>价格</th></tr><xsl:for-each select="catalog/cd"><tr><td><xsl:value-of select="title"/></td><xsl:choose><xsl:when test="price > 10"><td class="high"><xsl:value-of select="artist"/></td></xsl:when><xsl:when test="price > 9"><td class="middle"><xsl:value-of select="artist"/></td></xsl:when><xsl:otherwise><td class="ordinary"><xsl:value-of select="artist"/></td></xsl:otherwise></xsl:choose><td><xsl:value-of select="price"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 2个when和默认边界
本章附件与实例包
课程关联工程包(登记关联20章) · 含当前章节或实例 · mbskycom202611a542.zip
常见错误与边界检查
- 把API名称存在当作转换成功;把服务端输出冒称旧IE/ASP运行。
- match模式与XPath表达式混用,变量当可变赋值,文本排序冒充数值排序。
- 把结果树、序列化字节、实际计算样式或表单持久化混为一个结果。
技术规范与官方资料
练习与参考解答
查看参考解答
依据版本/上下文/处理器明确核对;不删失败样本或修改预期来掩盖实现差异。