xsl:for-each:上下文、过滤与全部比较分支
保留全量与按艺人筛选,并分别验证=、!=、小于、大于及边界。
学完这一章,你能够
- 保留全量与按艺人筛选,并分别验证=、!=、小于、大于及边界。
- 保留全量与按艺人筛选,并分别验证=、!=、小于、大于及边界。
选择每一项
for-each在选定节点集上建立每次迭代的当前上下文,title等相对路径随之改变;数据顺序和排序规则应明确。
本章分别生成三条全部记录及只匹配同一艺人的两条记录,空匹配输出零条。使用新的源树执行各例,避免前一次转换的空白处理影响后一次观察。
过滤的完整条件
=、!=、<、>的值比较按XPath1.0类型转换规则,不是全部JS严格比较;多个节点的比较还可能具有存在性语义。XML属性中的<需要写成<,而表达式符号本身属于XPath。
示例价格分别9、10、11,独立验证小于10/等于10/不等于10/大于10,不以一句支持过滤替代各个分支。
本章全部转换分支与源码对应
| 项目 | 写法/条件 | 用途与结果 | 限制/版本 |
| 1 | styles/for-all.xsl | 所有三条 | 完整预期在本章checks逐项断言 |
| 2 | styles/filter-artist.xsl | artist谓词结果 | 完整预期在本章checks逐项断言 |
| 3 | styles/filter-equal.xsl | equal谓词结果 | 完整预期在本章checks逐项断言 |
| 4 | styles/filter-not-equal.xsl | not-equal谓词结果 | 完整预期在本章checks逐项断言 |
| 5 | styles/filter-less.xsl | less谓词结果 | 完整预期在本章checks逐项断言 |
| 6 | styles/filter-greater.xsl | greater谓词结果 | 完整预期在本章checks逐项断言 |
| 7 | styles/filter-none.xsl | none谓词结果 | 完整预期在本章checks逐项断言 |
本章独立核心源码与验证步骤
展开查看收起
python
from xslt_lab import run_cases
# 蓝色天空网 mbsky.com:本章完整输入、规则与可观察预期。
SCENARIOS = [{'id': '1',
'style': 'styles/for-all.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 4.0}],
'goal': '所有三条'},
{'id': '2',
'style': 'styles/filter-artist.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 3.0}],
'goal': 'artist谓词结果'},
{'id': '3',
'style': 'styles/filter-equal.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 2.0}],
'goal': 'equal谓词结果'},
{'id': '4',
'style': 'styles/filter-not-equal.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 3.0}],
'goal': 'not-equal谓词结果'},
{'id': '5',
'style': 'styles/filter-less.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 2.0}],
'goal': 'less谓词结果'},
{'id': '6',
'style': 'styles/filter-greater.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 2.0}],
'goal': 'greater谓词结果'},
{'id': '7',
'style': 'styles/filter-none.xsl',
'input': 'data/catalog.xml',
'checks': [{'xpath': 'count(//table/tr)', 'expected': 1.0}],
'goal': 'none谓词结果'}]
if __name__=="__main__":
run_cases('xsl-for-each',SCENARIOS)
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 本章7个分支的实际结果应与各自checks一致,故意错误必须拒绝。
- 具体目标:所有三条;artist谓词结果;equal谓词结果;not-equal谓词结果;less谓词结果;greater谓词结果;none谓词结果
完整样式与指定输入:for-all.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><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="price"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 所有三条
完整样式与指定输入:filter-artist.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[artist='Z组']"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="price"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- artist谓词结果
完整样式与指定输入:filter-equal.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[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:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- equal谓词结果
完整样式与指定输入:filter-not-equal.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[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:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- not-equal谓词结果
完整样式与指定输入:filter-less.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[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:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- less谓词结果
完整样式与指定输入:filter-greater.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[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:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- greater谓词结果
完整样式与指定输入:filter-none.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[artist='没有']"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select="price"/></td></tr></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- none谓词结果
本章附件与实例包
课程关联工程包(登记关联20章) · 含当前章节或实例 · mbskycom202611a542.zip
常见错误与边界检查
- 把API名称存在当作转换成功;把服务端输出冒称旧IE/ASP运行。
- match模式与XPath表达式混用,变量当可变赋值,文本排序冒充数值排序。
- 把结果树、序列化字节、实际计算样式或表单持久化混为一个结果。
技术规范与官方资料
练习与参考解答
查看参考解答
依据版本/上下文/处理器明确核对;不删失败样本或修改预期来掩盖实现差异。