xsl:if:有效布尔值与价格边界
分别观察条件真、假和空选择,避免把字符串false误认为布尔false。
学完这一章,你能够
- 分别观察条件真、假和空选择,避免把字符串false误认为布尔false。
- 分别观察条件真、假和空选择,避免把字符串false误认为布尔false。
条件只有真分支
if的test必需,是XPath表达式,真时执行内部序列,无else子句;多分支用choose。它不只可放循环里,也可出现在允许的模板序列位置。
本例price>10只输出11,price=10单独命中10,price<10命中9;比较三个分支不混淆大于与大于等于。
XPath布尔转换
1.0非空字符串false仍是真,空节点集为假,字符串/数字/布尔的转换要按版本。缺失price转成数值NaN后比较不等于一个正常零值。
test属性要按XML转义,输出必须对应真实条件;不能只在说明写边界而代码用不同阈值。
语法、教学目标与适用条件
| 项目 | 写法/条件 | 用途与结果 | 限制/版本 |
|---|---|---|---|
| 大于/等于/小于 | price >10 / =10 / <10 | 分别11/10/9 | 测试边界而非只一条 |
| 非空字符串 | boolean("false") | true | 不是文本等于false即布尔假 |
| 空节点集 | catalog/missing | false | 没有输出不等于加载错误 |
本章全部转换分支与源码对应
| 项目 | 写法/条件 | 用途与结果 | 限制/版本 |
|---|---|---|---|
| 1 | styles/if-greater.xsl | greater10边界 | 完整预期在本章checks逐项断言 |
| 2 | styles/if-equal.xsl | equal10边界 | 完整预期在本章checks逐项断言 |
| 3 | styles/if-less.xsl | less10边界 | 完整预期在本章checks逐项断言 |
| 4 | styles/if-boolean.xsl | 非空字符串为真、空节点集假 | 完整预期在本章checks逐项断言 |
本章独立核心源码与验证步骤
from xslt_lab import run_cases
# 蓝色天空网 mbsky.com:本章完整输入、规则与可观察预期。
SCENARIOS = [{'id': '1',
'style': 'styles/if-greater.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': '//table/tr[position() > 1]/td[3]/text()', 'expected': ['11']}],
'goal': 'greater10边界'},
{'id': '2',
'style': 'styles/if-equal.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': '//table/tr[position() > 1]/td[3]/text()', 'expected': ['10']}],
'goal': 'equal10边界'},
{'id': '3',
'style': 'styles/if-less.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': '//table/tr[position() > 1]/td[3]/text()', 'expected': ['9']}],
'goal': 'less10边界'},
{'id': '4',
'style': 'styles/if-boolean.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(/out/string-true)', 'expected': 1.0},
{'xpath': 'count(/out/missing-true)', 'expected': 0.0}],
'goal': '非空字符串为真、空节点集假'}]
if __name__=="__main__":
run_cases('xsl-if',SCENARIOS)
所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:9a31ac0e9246e0a1c7ee73c27dcfb6d54475c04fe407f94b3c864e013441ac29
- 本章4个分支的实际结果应与各自checks一致,故意错误必须拒绝。
- 具体目标:greater10边界;equal10边界;less10边界;非空字符串为真、空节点集假
完整样式与指定输入:if-greater.xsl
<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"><xsl:if test="price > 10"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="price"/></td></tr></xsl:if></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:12730b479bde821ae6eb4e4283473dc9dc898c8aadd9182f8b51ca5522667a64
- greater10边界
完整样式与指定输入:if-equal.xsl
<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"><xsl:if test="price = 10"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="price"/></td></tr></xsl:if></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:8ddd26c78193ac4db2f8b57e8941d174c01a5669f72ffe8fb3f7fbd4fd7091d8
- equal10边界
完整样式与指定输入:if-less.xsl
<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"><xsl:if test="price < 10"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="price"/></td></tr></xsl:if></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:86cf5db61f4ee7b1becb8fab4c5b6263e76a708f1d7f7140267d191d0c4099d1
- less10边界
完整样式与指定输入:if-boolean.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method="xml" encoding="UTF-8"/><xsl:template match="/"><out><xsl:if test="'false'"><string-true/></xsl:if><xsl:if test="catalog/missing"><missing-true/></xsl:if></out></xsl:template></xsl:stylesheet>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:2843dd64aef0521b2802538401351638c1436874e075dcd18715062a4e38bae6
- 非空字符串为真、空节点集假
常见错误与边界
- 把API名称存在当作转换成功;把服务端输出冒称旧IE/ASP运行。
- match模式与XPath表达式混用,变量当可变赋值,文本排序冒充数值排序。
- 把结果树、序列化字节、实际计算样式或表单持久化混为一个结果。
练习与解答
选择本章一个明确输入,只改一个条件,先写预期再运行;保留9/10/11等边界。
展开参考解答
依据版本/上下文/处理器明确核对;不删失败样本或修改预期来掩盖实现差异。