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逐项断言 |
本章独立核心源码与验证步骤
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)
所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:0125d9ee87e160b0ac0c8e79d89596256de4e4b7a3b0ac8a2a4bd636947d464e
- 本章7个分支的实际结果应与各自checks一致,故意错误必须拒绝。
- 具体目标:所有三条;artist谓词结果;equal谓词结果;not-equal谓词结果;less谓词结果;greater谓词结果;none谓词结果
完整样式与指定输入:for-all.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"><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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:00a010bf2cc7ca10c36041e5711678c733703686c06462b5aacccac601df06e8
- 所有三条
完整样式与指定输入:filter-artist.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[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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:1e8dc8104ba70b89de1853c17df54577c267f24bd95139267a2af928a3ca2f86
- artist谓词结果
完整样式与指定输入:filter-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[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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:899b146d257ca6f16c3f2aae155185212ad6d2936a351a7ccaf0ed20711e3082
- equal谓词结果
完整样式与指定输入:filter-not-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[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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:895628f239593f417747913371644d43d1f97fb963448815e28238b428abda0c
- not-equal谓词结果
完整样式与指定输入:filter-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[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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:f35fc5bd6c3fa695dcd0d02bd92a8a89bd82ae817323fa370f818562794a38b5
- less谓词结果
完整样式与指定输入:filter-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[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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:399fd57fce6a723d90877b30e1a010eaf8b097ba5ce15c3c818a40f59b3bfb2f
- greater谓词结果
完整样式与指定输入:filter-none.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[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>所需环境:Windows / Python3.12+lxml6.1.3 / SaxonC12.9.0 / Edge156 / PHP8.4.27(独立附例)
当前源码 SHA256:e446d5dda72390d1b983c2c17bd5c36d8c154927c19940e42fe82bda36b6dcad
- none谓词结果
常见错误与边界
- 把API名称存在当作转换成功;把服务端输出冒称旧IE/ASP运行。
- match模式与XPath表达式混用,变量当可变赋值,文本排序冒充数值排序。
- 把结果树、序列化字节、实际计算样式或表单持久化混为一个结果。
练习与解答
选择本章一个明确输入,只改一个条件,先写预期再运行;保留9/10/11等边界。
展开参考解答
依据版本/上下文/处理器明确核对;不删失败样本或修改预期来掩盖实现差异。