ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

XSLT-if详解:条件判断的语法与实战

XSLT-if详解:条件判断的语法与实战 1. 引言XSLT可扩展样式表语言转换是处理 XML 文档的强大工具而xsl:if是其中最常用的条件判断指令之一。它允许我们在转换过程中根据节点内容、属性值或表达式结果有选择地输出内容。本文将通过丰富的代码实例深入讲解xsl:if的语法、用法、常见陷阱以及与其他条件指令的对比。2. 基础语法xsl:if的语法非常简单核心是test属性它接受一个 XPath 表达式。当表达式的结果为真true时元素内部的内容会被输出否则被忽略。xsl:if test条件表达式 !-- 当条件为真时输出的内容 -- /xsl:if下面是一个最基础的示例。假设我们有如下 XML 数据?xml version1.0 encodingUTF-8? books book titleXML 入门/title price59.00/price /book book titleXSLT 高级编程/title price89.00/price /book /books我们希望只输出价格大于 60 元的书籍?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html body h2高价书籍列表/h2 xsl:for-each selectbooks/book xsl:if testprice 60 p xsl:value-of selecttitle/ —— 价格xsl:value-of selectprice/ 元 /p /xsl:if /xsl:for-each /body /html /xsl:template /xsl:stylesheet运行结果html body h2高价书籍列表/h2 pXSLT 高级编程 —— 价格89.00 元/p /body /html注意在 XSLT 1.0 中需要写成gt;需要写成lt;以避免与 XML 标签冲突。3. 常用条件表达式xsl:if的test属性支持丰富的 XPath 表达式。下面分类介绍最常见的几种。3.1 比较运算数值和字符串比较是最基础的条件判断。!-- 数值比较 -- xsl:if testprice 100高价书/xsl:if xsl:if testprice 50低价书/xsl:if xsl:if testprice 59.00价格正好是 59 元/xsl:if !-- 字符串比较 -- xsl:if testtitle XML 入门找到入门书/xsl:if xsl:if testtitle ! XSLT 高级编程不是高级编程书/xsl:if3.2 逻辑运算使用and、or、not()可以组合多个条件。!-- 与价格在 50 到 100 之间 -- xsl:if testprice 50 and price 100 中等价位书籍 /xsl:if !-- 或作者是张三或李四 -- xsl:if testauthor 张三 or author 李四 目标作者的作品 /xsl:if !-- 非不是绝版书 -- xsl:if testnot(status out_of_print) 在售书籍 /xsl:if3.3 节点存在性判断判断某个子元素或属性是否存在是实际开发中非常常见的需求。!-- 判断子元素是否存在 -- xsl:if testpublisher 有出版社信息xsl:value-of selectpublisher/ /xsl:if !-- 判断属性是否存在 -- xsl:if testisbn ISBNxsl:value-of selectisbn/ /xsl:if !-- 判断节点集合是否为空 -- xsl:if testnot(comments/comment) 暂无评论 /xsl:if3.4 字符串函数XPath 提供了丰富的字符串处理函数可以用于更复杂的条件判断。!-- 判断字符串长度 -- xsl:if teststring-length(title) 10 标题较长 /xsl:if !-- 判断是否以某字符串开头 -- xsl:if teststarts-with(title, XML) 标题以 XML 开头 /xsl:if !-- 判断是否包含某字符串 -- xsl:if testcontains(description, 实战) 包含实战关键词 /xsl:if4. 实战案例图书目录转换下面通过一个完整的实战案例综合运用xsl:if的各种用法。假设我们要将一份图书 XML 转换为带分类标记的 HTML 页面。?xml version1.0 encodingUTF-8? library book categoryprogramming titleXSLT 2.0 权威指南/title authorMichael Kay/author price currencyCNY128.00/price stock25/stock /book book categorydatabase titleSQL 必知必会/title authorBen Forta/author price currencyCNY49.00/price stock0/stock /book book categoryprogramming title深入理解计算机系统/title authorRandal E. Bryant/author price currencyCNY139.00/price stock8/stock /book /library对应的 XSLT 样式表?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html head title图书馆藏书/title /head body h1图书馆藏书目录/h1 xsl:for-each selectlibrary/book div styleborder:1px solid #ccc; margin:10px; padding:10px; h3xsl:value-of selecttitle//h3 p作者xsl:value-of selectauthor//p p价格xsl:value-of selectprice/ 元/p !-- 分类标记 -- xsl:if testcategory programming p stylecolor:blue;分类编程技术/p /xsl:if xsl:if testcategory database p stylecolor:green;分类数据库/p /xsl:if !-- 库存状态提示 -- xsl:if teststock 0 p stylecolor:red;暂时缺货/p /xsl:if xsl:if teststock 0 and stock 10 p stylecolor:orange;库存紧张仅剩 xsl:value-of selectstock/ 本/p /xsl:if xsl:if teststock 10 p stylecolor:green;库存充足/p /xsl:if !-- 价格区间提示 -- xsl:if testprice 100 p这是一本高价技术书籍/p /xsl:if /div /xsl:for-each /body /html /xsl:template /xsl:stylesheet输出结果html head title图书馆藏书/title /head body h1图书馆藏书目录/h1 div styleborder:1px solid #ccc; margin:10px; padding:10px; h3XSLT 2.0 权威指南/h3 p作者Michael Kay/p p价格128.00 元/p p stylecolor:blue;分类编程技术/p p stylecolor:green;库存充足/p p这是一本高价技术书籍/p /div div styleborder:1px solid #ccc; margin:10px; padding:10px; h3SQL 必知必会/h3 p作者Ben Forta/p p价格49.00 元/p p stylecolor:green;分类数据库/p p stylecolor:red;暂时缺货/p /div div styleborder:1px solid #ccc; margin:10px; padding:10px; h3深入理解计算机系统/h3 p作者Randal E. Bryant/p p价格139.00 元/p p stylecolor:blue;分类编程技术/p p stylecolor:orange;库存紧张仅剩 8 本/p p这是一本高价技术书籍/p /div /body /html5. 嵌套使用xsl:if支持嵌套可以在一个条件内部再判断其他条件实现更复杂的逻辑。xsl:for-each selectlibrary/book xsl:if testcategory programming xsl:if testprice 100 p高价编程书xsl:value-of selecttitle//p /xsl:if xsl:if testprice 100 p平价编程书xsl:value-of selecttitle//p /xsl:if /xsl:if /xsl:for-each虽然嵌套可以实现多分支逻辑但当分支较多时代码会变得难以阅读。此时建议使用xsl:choose配合xsl:when和xsl:otherwise结构更清晰。6. 与 xsl:choose 的对比xsl:if只有真和假两个分支且没有否则else的概念。当需要多分支或默认分支时应使用xsl:choose。!-- 使用 xsl:if 实现多分支代码冗余 -- xsl:if testscore 90优秀/xsl:if xsl:if testscore 80 and score 90良好/xsl:if xsl:if testscore 60 and score 80及格/xsl:if xsl:if testscore 60不及格/xsl:if !-- 使用 xsl:choose 实现同样逻辑更清晰 -- xsl:choose xsl:when testscore 90优秀/xsl:when xsl:when testscore 80良好/xsl:when xsl:when testscore 60及格/xsl:when xsl:otherwise不及格/xsl:otherwise /xsl:choose选择建议只有单一条件判断时使用xsl:if更简洁。需要否则分支或多条件互斥时使用xsl:choose更合适。多个独立条件需要分别判断时可以连续使用多个xsl:if。7. 常见陷阱与注意事项在实际开发中xsl:if有几个容易踩坑的地方需要特别注意。7.1 布尔值转换XPath 中空节点集、空字符串、数字 0 和 NaN 都会被转换为 false其他值转换为 true。这可能导致意想不到的结果。!-- 陷阱price 为 0 时条件为 false -- xsl:if testprice 价格存在且不为 0 /xsl:if !-- 正确做法显式判断节点是否存在 -- xsl:if testprice ! 价格节点存在 /xsl:if7.2 字符串比较的引号问题在 XML 属性中字符串值通常使用单引号包裹避免与属性值的双引号冲突。!-- 正确外层双引号内层单引号 -- xsl:if testtitle XML 入门.../xsl:if !-- 错误内外都是双引号会导致解析错误 -- !-- xsl:if testtitle XML 入门.../xsl:if --7.3 大小写敏感XPath 的字符串比较是大小写敏感的。如果需要忽略大小写可以使用translate()函数转换。!-- 大小写敏感 -- xsl:if testcategory Programming.../xsl:if !-- 忽略大小写比较 -- xsl:if testtranslate(category, ABCDEFGHIJKLMNOPQRSTUVWXYZ, abcdefghijklmnopqrstuvwxyz) programming 编程类书籍不区分大小写 /xsl:if7.4 空白字符处理XML 中的空白字符可能导致字符串比较失败。使用normalize-space()函数可以去除首尾空白并合并连续空白。!-- 假设 category 的值为 programming 带空格 -- xsl:if testnormalize-space(category) programming 编程类书籍 /xsl:if8. XSLT 2.0 中的增强XSLT 2.0 对条件判断做了一些增强最显著的是支持if...then...else表达式可以直接在 XPath 中使用。!-- XSLT 2.0 的 if-then-else 表达式 -- xsl:value-of selectif (price 100) then 高价 else 平价/ !-- 也可以嵌套使用 -- xsl:value-of selectif (price 100) then 高价 else if (price 50) then 中等 else 低价/此外XSLT 2.0 还支持在xsl:if中使用更丰富的 XPath 2.0 函数如matches()正则匹配、ends-with()等。!-- XSLT 2.0 正则匹配 -- xsl:if testmatches(title, ^XSLT) 标题以 XSLT 开头 /xsl:if !-- XSLT 2.0 结尾判断 -- xsl:if testends-with(title, 指南) 标题以指南结尾 /xsl:if9. 总结xsl:if是 XSLT 中最基础也最常用的条件判断指令。通过本文的实例我们掌握了它的核心语法、常用表达式、嵌套用法以及与xsl:choose的对比。在实际开发中建议根据场景选择合适的条件指令单一条件用xsl:if多分支互斥用xsl:chooseXSLT 2.0 环境下还可以使用内联的if-then-else表达式。同时要注意布尔值转换、引号、大小写和空白字符等常见陷阱写出健壮可靠的样式表。
返回列表