ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

XSLT 实例、元素与转换:从入门到实战

XSLT 实例、元素与转换:从入门到实战 1. 引言XSLT可扩展样式表语言转换是一种用于将 XML 文档转换为其他格式如 HTML、文本或其他 XML的语言。它通过 XSLT 样式表定义转换规则将源 XML 树映射为目标输出树。本文将通过丰富的代码实例系统讲解 XSLT 的核心元素、转换机制和实战应用。2. XSLT 基础概念XSLT 样式表本身是一个 XML 文档根元素为xsl:stylesheet或xsl:transform。它通过 XPath 表达式定位 XML 节点并使用模板规则描述如何转换这些节点。一个最简单的 XSLT 样式表结构如下?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html body h2Hello, XSLT!/h2 /body /html /xsl:template /xsl:stylesheet上述样式表匹配文档根节点输出一个简单的 HTML 页面。下面我们通过一个完整的实例来理解 XSLT 的工作流程。3. XSLT 实例图书列表转换假设我们有一个描述图书信息的 XML 文档希望通过 XSLT 将其转换为 HTML 表格展示。3.1 源 XML 文档?xml version1.0 encodingUTF-8? bookstore book category编程 title langzhXML 入门与实践/title author张三/author price59.00/price /book book category数据库 title langzhSQL 高级查询/title author李四/author price79.00/price /book book category编程 title langenXSLT Cookbook/title author王五/author price99.00/price /book /bookstore3.2 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 h2图书信息一览/h2 table border1 tr th书名/th th作者/th th价格/th th分类/th /tr xsl:for-each selectbookstore/book tr tdxsl:value-of selecttitle//td tdxsl:value-of selectauthor//td tdxsl:value-of selectprice//td tdxsl:value-of selectcategory//td /tr /xsl:for-each /table /body /html /xsl:template /xsl:stylesheet3.3 转换结果上述样式表通过xsl:for-each遍历所有book元素并使用xsl:value-of提取子元素和属性的值最终生成如下 HTML 表格html head title图书列表/title /head body h2图书信息一览/h2 table border1 tr th书名/th th作者/th th价格/th th分类/th /tr tr tdXML 入门与实践/td td张三/td td59.00/td td编程/td /tr tr tdSQL 高级查询/td td李四/td td79.00/td td数据库/td /tr tr tdXSLT Cookbook/td td王五/td td99.00/td td编程/td /tr /table /body /html4. XSLT 核心元素详解XSLT 提供了丰富的元素用于构建转换规则。下面逐一介绍最常用的核心元素并给出可运行的代码实例。4.1 xsl:template 与 xsl:apply-templatesxsl:template定义转换规则xsl:apply-templates将模板应用于匹配的节点。这是 XSLT 最核心的递归处理机制。?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html body xsl:apply-templates selectbookstore/book/ /body /html /xsl:template xsl:template matchbook p strongxsl:value-of selecttitle//strong 作者xsl:value-of selectauthor/ 价格xsl:value-of selectprice/ /p /xsl:template /xsl:stylesheet当根模板匹配到bookstore/book节点时会自动调用匹配book的模板实现节点级递归处理。4.2 xsl:value-of 与 xsl:textxsl:value-of用于提取节点或表达式的字符串值xsl:text用于输出纯文本内容可精确控制空白字符。xsl:template matchbook p xsl:text书名/xsl:text xsl:value-of selecttitle/ xsl:text作者/xsl:text xsl:value-of selectauthor/ xsl:text/xsl:text /p /xsl:template4.3 xsl:for-each 与排序xsl:for-each用于循环遍历节点集可配合xsl:sort实现排序输出。xsl:template match/ ul xsl:for-each selectbookstore/book xsl:sort selectprice>xsl:template matchbook p xsl:value-of selecttitle/ xsl:if testprice 80 高价图书 /xsl:if /p /xsl:templatexsl:template matchbook p xsl:value-of selecttitle/ xsl:choose xsl:when testprice 80 高价图书 /xsl:when xsl:when testprice 60 中价图书 /xsl:when xsl:otherwise 低价图书 /xsl:otherwise /xsl:choose /p /xsl:template4.5 xsl:attribute 与 xsl:element 动态创建xsl:attribute动态添加属性xsl:element动态创建元素适用于输出结构不固定的场景。xsl:template matchbook xsl:element namediv xsl:attribute nameclass xsl:choose xsl:when testprice 80high-price/xsl:when xsl:otherwisenormal-price/xsl:otherwise /xsl:choose /xsl:attribute xsl:value-of selecttitle/ /xsl:element /xsl:template4.6 xsl:variable 与 xsl:param 变量和参数xsl:variable定义局部变量xsl:param定义可被外部传入的模板参数。xsl:template match/ xsl:variable namediscount select0.8/ xsl:for-each selectbookstore/book p xsl:value-of selecttitle/ 折后价xsl:value-of selectprice * $discount/ /p /xsl:for-each /xsl:templatexsl:template namebook-item xsl:param nameshowPrice selecttrue/ p xsl:value-of selecttitle/ xsl:if test$showPrice true xsl:value-of selectprice/ 元 /xsl:if /p /xsl:template4.7 xsl:call-template 与命名模板xsl:call-template按名称调用模板实现代码复用。xsl:template match/ html body xsl:for-each selectbookstore/book xsl:call-template namebook-item/ /xsl:for-each /body /html /xsl:template xsl:template namebook-item p xsl:value-of selecttitle/ 作者xsl:value-of selectauthor/ /p /xsl:template4.8 xsl:number 自动编号xsl:number为节点自动编号常用于生成目录或列表序号。xsl:template matchbook p xsl:number valueposition() format1. / xsl:value-of selecttitle/ /p /xsl:template4.9 xsl:copy 与 xsl:copy-of 节点复制xsl:copy复制当前节点不含子节点xsl:copy-of复制节点及其全部子树。xsl:template matchbook xsl:copy xsl:attribute namesourcebookstore/xsl:attribute xsl:copy-of selecttitle/ /xsl:copy /xsl:template4.10 xsl:output 输出格式控制xsl:output声明输出文档的类型、编码和缩进方式。xsl:output methodhtml encodingUTF-8 indentyes/常用 method 取值包括xml、html和text。5. XSLT 转换实战XML 转 HTML 报表下面综合运用上述元素实现一个完整的 XML 转 HTML 报表案例。源数据为员工信息要求输出带统计信息的报表。5.1 源 XML?xml version1.0 encodingUTF-8? company department name研发部 employee name赵一/name position高级工程师/position salary25000/salary /employee employee name钱二/name position工程师/position salary18000/salary /employee /department department name市场部 employee name孙三/name position市场专员/position salary12000/salary /employee /department /company5.2 XSLT 样式表?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:output methodhtml encodingUTF-8 indentyes/ xsl:template match/ html head title员工报表/title /head body h2公司员工报表/h2 xsl:apply-templates selectcompany/department/ hr/ p strong员工总数/strong xsl:value-of selectcount(company/department/employee)/ /p p strong平均薪资/strong xsl:value-of selectformat-number(sum(company/department/employee/salary) div count(company/department/employee), ¥#,##0.00)/ /p /body /html /xsl:template xsl:template matchdepartment h3xsl:value-of selectname//h3 table border1 tr th序号/th th姓名/th th职位/th th薪资/th /tr xsl:for-each selectemployee tr tdxsl:number valueposition() format1//td tdxsl:value-of selectname//td tdxsl:value-of selectposition//td tdxsl:value-of selectformat-number(salary, ¥#,##0)//td /tr /xsl:for-each /table /xsl:template /xsl:stylesheet5.3 关键点说明count()函数统计节点数量。sum()函数对节点集求和。format-number()函数格式化数字为货币样式。xsl:number为每行生成序号。6. XSLT 转换实战XML 转 XML 数据重组XSLT 也常用于 XML 到 XML 的结构重组例如将扁平结构转换为嵌套结构。6.1 源 XML扁平结构?xml version1.0 encodingUTF-8? orders order id1001 customer张三 product笔记本电脑 quantity1/ order id1002 customer李四 product鼠标 quantity2/ order id1003 customer张三 product键盘 quantity1/ /orders6.2 XSLT 样式表按客户分组?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:output methodxml encodingUTF-8 indentyes/ xsl:key namecustomer-key matchorder usecustomer/ xsl:template match/ customer-orders xsl:for-each selectorders/order[generate-id() generate-id(key(customer-key, customer)[1])] xsl:sort selectcustomer/ customer name{customer} xsl:for-each selectkey(customer-key, customer) order id{id} product{product} quantity{quantity}/ /xsl:for-each /customer /xsl:for-each /customer-orders /xsl:template /xsl:stylesheet6.3 转换结果?xml version1.0 encodingUTF-8? customer-orders customer name张三 order id1001 product笔记本电脑 quantity1/ order id1003 product键盘 quantity1/ /customer customer name李四 order id1002 product鼠标 quantity2/ /customer /customer-orders这里使用xsl:key和generate-id()实现分组是 XSLT 1.0 中常用的分组技巧。7. XSLT 转换实战XML 转纯文本当输出格式为纯文本时使用methodtext常用于生成配置文件或日志。?xml version1.0 encodingUTF-8? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:output methodtext encodingUTF-8/ xsl:template match/ xsl:text图书清单 /xsl:text xsl:text /xsl:text xsl:for-each selectbookstore/book xsl:value-of selecttitle/ xsl:text | /xsl:text xsl:value-of selectauthor/ xsl:text | /xsl:text xsl:value-of selectprice/ xsl:text 元 /xsl:text /xsl:for-each /xsl:template /xsl:stylesheet输出结果为图书清单 XML 入门与实践 | 张三 | 59.00 元 SQL 高级查询 | 李四 | 79.00 元 XSLT Cookbook | 王五 | 99.00 元8. 常见问题与调试技巧8.1 命名空间处理当源 XML 使用命名空间时样式表中必须声明相同的命名空间前缀并在 XPath 中使用该前缀。xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:bkhttp://www.example.com/bookstore xsl:template match/ xsl:for-each selectbk:bookstore/bk:book pxsl:value-of selectbk:title//p /xsl:for-each /xsl:template /xsl:stylesheet8.2 调试建议使用indentyes让输出更易读。先用简单模板验证 XPath 路径是否正确。利用xsl:message输出调试信息到控制台。分步转换先输出 XML再转换为 HTML便于定位问题。xsl:template matchbook xsl:message正在处理图书xsl:value-of selecttitle//xsl:message pxsl:value-of selecttitle//p /xsl:template9. 总结本文通过丰富的代码实例系统介绍了 XSLT 的核心元素与转换机制。从最简单的模板匹配到for-each、choose、variable、key等高级特性再到 XML 转 HTML、XML 转 XML、XML 转文本三种典型场景覆盖了 XSLT 开发的主要知识点。掌握这些内容后读者可以灵活运用 XSLT 完成各类数据转换任务。
RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进