XSLT 元素参考手册(2025 终极速查版)
按实际使用频率排序,带版本标记 + 一句话说明 + 必背示例
| 频率 | 元素 | 版本 | 所属类别 | 一句话作用 | 最小必背示例 |
|---|---|---|---|---|---|
| 1 | <xsl:template> | 1.0+ | 核心 | 定义一个模板规则 | <xsl:template match="book"> |
| 2 | <xsl:apply-templates> | 1.0+ | 核心 | 触发模板匹配(数据驱动) | <xsl:apply-templates select="book"/> |
| 3 | <xsl:value-of> | 1.0+ | 输出 | 取出节点文本值 | <xsl:value-of select="title"/> |
| 4 | <xsl:copy-of> | 1.0+ | 复制 | 深度复制整个节点树 | <xsl:copy-of select="."/> |
| 5 | <xsl:copy> | 1.0+ | 复制 | 浅复制当前节点(常用于身份模板) | <xsl:copy><xsl:apply-templates/></xsl:copy> |
| 6 | <xsl:if> | 1.0+ | 条件 | 单条件判断 | <xsl:if test="price > 100"> |
| 7 | <xsl:choose> / <xsl:when> / <xsl:otherwise> | 1.0+ | 条件 | 多条件分支 | <xsl:choose><xsl:when test="@cat='A'"> |
| 8 | <xsl:for-each> | 1.0+ | 循环 | 显式循环(不推荐多用) | <xsl:for-each select="book"> |
| 9 | <xsl:sort> | 1.0+ | 排序 | 在 apply-templates / for-each 里排序 | <xsl:sort select="price" data-type="number" order="descending"/> |
| 10 | <xsl:element> | 1.0+ | 动态创建 | 动态生成元素名 | <xsl:element name="{$tagName}"> |
| 11 | <xsl:attribute> | 1.0+ | 动态创建 | 动态生成属性 | <xsl:attribute name="class">red</xsl:attribute> |
| 12 | <xsl:text> | 1.0+ | 输出 | 输出纯文本(避免多余空白) | <xsl:text> </xsl:text> |
| 13 | <xsl:output> | 1.0+ | 顶层 | 声明输出方式(html/xml/json/text) | <xsl:output method="html" indent="yes"/> |
| 14 | <xsl:param> / <xsl:variable> | 1.0+ | 变量参数 | 声明参数或变量 | <xsl:param name="lang" select="'zh'"/> |
| 15 | <xsl:call-template> | 1.0+ | 调用 | 按名字调用模板(函数式) | <xsl:call-template name="format-price"/> |
| 16 | <xsl:with-param> | 1.0+ | 参数传递 | 向 call-template 传参 | <xsl:with-param name="p" select="$price"/> |
| 17 | <xsl:include> / <xsl:import> | 1.0+ | 模块化 | 引入其他样式表(import 优先级更低) | <xsl:include href="common.xsl"/> |
| 18 | <xsl:stylesheet> / <xsl:transform> | 1.0+ | 根元素 | 文档根元素(两者完全等价) | <xsl:stylesheet version="3.0"> |
| 19 | <xsl:mode>(XSLT 3.0) | 3.0 | 模式 | 声明模板模式(最重要的新特性) | <xsl:mode on-no-match="shallow-copy"/> |
| 20 | <xsl:result-document> | 2.0+ | 多输出 | 输出到多个文件(拆分神器) | <xsl:result-document href="out/{@id}.xml"> |
| 21 | <xsl:function>(XSLT 2.0+) | 2.0+ | 函数 | 定义可复用函数 | <xsl:function name="my:price" as="xs:decimal"> |
| 22 | <xsl:character-map> | 2.0+ | 输出 | 自定义字符转义(常用于禁用某些实体) | <xsl:character-map name="no-nbsp"><xsl:output-character character=" " string=" "/></xsl:character-map> |
| 23 | <xsl:analyze-string> | 2.0+ | 正则 | 正则表达式匹配替换 | <xsl:analyze-string select="$text" regex="\d+"> |
| 24 | <xsl:merge>(XSLT 3.0) | 3.0 | 合并 | 多源合并(类似 SQL join) | <xsl:merge><xsl:merge-source select="collection(...)"/> |
| 25 | <xsl:evaluate>(XSLT 3.0) | 3.0 | 动态 XPath | 运行时动态执行 XPath 字符串 | <xsl:evaluate xpath="$dynamicPath"/> |
2025 年必背 10 个组合(记住这 10 个,天下我有)
| 用途 | 代码模板(直接复制) |
|---|---|
| 1. 身份模板(编辑 XML 起点) | <xsl:mode on-no-match="shallow-copy"/>(3.0)或 1.0 版: <xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:template> |
| 2. 删除节点 | <xsl:template match="要删除的节点"/> |
| 3. 修改属性值 | <xsl:template match="book/@price"><xsl:attribute name="price"><xsl:value-of select=". * 1.1"/></xsl:attribute></xsl:template> |
| 4. 添加节点 | <xsl:template match="/root"><xsl:copy><xsl:copy-of select="@*|node()"/><created><xsl:value-of select="current-dateTime()"/></created></xsl:copy></xsl:template> |
| 5. 排序 | <xsl:apply-templates select="book"><xsl:sort select="price" data-type="number" order="descending"/></xsl:apply-templates> |
| 6. 条件输出 | <xsl:choose><xsl:when test="price>100"><expensive/></xsl:when><xsl:otherwise><cheap/></xsl:otherwise></xsl:choose> |
| 7. XML → JSON(3.0 最简) | <xsl:value-of select="xml-to-json(.)"/>(只需加 xpath-default-namespace 即可) |
| 8. 拆分文件 | <xsl:template match="book"><xsl:result-document href="books/{@id}.xml"><xsl:copy-of select="."/></xsl:result-document></xsl:template> |
| 9. 传参调用模板 | <xsl:call-template name="header"><xsl:with-param name="title" select="'我的书架'"/></xsl:call-template> |
| 10. 递归处理树 | <xsl:template match="folder"><details><summary><xsl:value-of select="@name"/></summary><ul><xsl:apply-templates/></ul></details></xsl:template> |
这张表打印出来贴墙上,基本覆盖你未来 10 年所有 XSLT 需求。
需要我给你生成 PDF/Excel/Markdown 可打印版吗?一键发你!