XSLT 实例

XSLT 终极实战实例合集(2025 年最新、可直接复制运行)

下面给出 10 个真实世界最常用、最能体现 XSLT 威力的完整实例,全部用 XSLT 3.0 + Saxon 写成(降级到 1.0 也很容易)。

每个例子都包含:

  • 输入 XML
  • XSLT 代码
  • 输出结果
  • 一句话说明用途

直接复制就能跑!

1. XML → 响应式 HTML 表格(最经典网页渲染)

<!-- books.xml -->
<library>
  <book id="b1" category="IT">
    <title>XSLT 权威指南</title><author>张三</author><price>89.00</price><year>2024</year>
  </book>
  <book id="b2" category="小说">
    <title>三体</title><author>刘慈欣</author><price>38.00</price><year>2008</year>
  </book>
</library>
<!-- html-table.xsl -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>

<xsl:template match="/library">
  <html>
    <head>
      <meta charset="utf-8"/>
      <title>我的书架</title>
      <style>
        table { width:100%; border-collapse:collapse; }
        th, td { border:1px solid #ccc; padding:8px; text-align:left; }
        tr:nth-child(even) { background:#f9f9f9; }
        .IT { color:blue; } .小说 { color:green; }
      </style>
    </head>
    <body>
      <h1>书架(<xsl:value-of select="count(book)"/> 本)</h1>
      <table>
        <tr><th>ID</th><th>标题</th><th>作者</th><th>价格</th><th>分类</th></tr>
        <xsl:for-each select="book">
          <xsl:sort select="price" data-type="number" order="descending"/>
          <tr>
            <td><xsl:value-of select="@id"/></td>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="author"/></td>
            <td>¥<xsl:value-of select="format-number(price, '###,##0.00')"/></td>
            <td class="{@category}"><xsl:value-of select="@category"/></td>
          </tr>
        </xsl:for-each>
      </table>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>

运行结果:一个带排序、样式、千位分隔符的完整 HTML 页面。

2. XML → JSON(XSLT 3.0 原生支持)

<!-- to-json.xsl -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/library">
  <xsl:text>{"books":[</xsl:text>
  <xsl:for-each select="book">
    <xsl:if test="position() > 1">,</xsl:if>
    {
      "id": "<xsl:value-of select="@id"/>",
      "title": "<xsl:value-of select="title"/>",
      "price": <xsl:value-of select="price"/>
    }
  </xsl:for-each>
  <xsl:text>]}</xsl:text>
</xsl:template>
</xsl:stylesheet>

更优雅写法(XSLT 3.0):

<xsl:value-of select="map{
  'books': array{ book ! map{
    'id': string(@id),
    'title': string(title),
    'price': number(price)
  }}
}" separator="" json-output="yes"/>

3. 万能 XML 编辑器(增删改查一条龙)

<!-- edit-xml.xsl -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- 复制一切 -->
  <xsl:mode on-no-match="shallow-copy"/>

  <!-- 删除价格大于100的书 -->
  <xsl:template match="book[price > 100]"/>

  <!-- 把所有书加一个 status 属性 -->
  <xsl:template match="book">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="status">在售</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- 在根节点加更新时间 -->
  <xsl:template match="/library">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <updated><xsl:value-of select="current-dateTime()"/></updated>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

4. 一个 XML 拆成多个文件(XSLT 3.0)

<!-- split.xsl -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="book">
    <xsl:result-document href="output/book-{@id}.xml">
      <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <xsl:copy-of select="."/>
      </book>
    </xsl:result-document>
  </xsl:template>

</xsl:stylesheet>

5. 带参数的通用转换(命令行传参)

<!-- param.xsl -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="min-price" as="xs:decimal" select="0"/>

<xsl:template match="/library">
  <cheap-books>
    <xsl:copy-of select="book[price &lt;= $min-price]"/>
  </cheap-books>
</xsl:template>
</xsl:stylesheet>

运行:

java -jar saxon-he-12.jar -s:books.xml -xsl:param.xsl -o:cheap.xml min-price=50

6. 递归生成目录树(菜单、文件结构)

<!-- tree.xml -->
<folder name="项目">
  <folder name="src">
    <file name="main.java"/>
    <file name="util.java"/>
  </folder>
  <folder name="docs">
    <file name="readme.md"/>
  </folder>
</folder>
<!-- tree-html.xsl -->
<xsl:template match="folder">
  <details><summary><xsl:value-of select="@name"/></summary>
    <ul>
      <xsl:apply-templates select="folder|file"/>
    </ul>
  </details>
</xsl:template>

<xsl:template match="file">
  <li><xsl:value-of select="@name"/></li>
</xsl:template>

7. CSV ←→ XML 互相转换

<!-- csv-to-xml.xsl -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="csv" select="unparsed-text('data.csv')"/>

<xsl:template name="xsl:initial-template">
  <books>
    <xsl:for-each select="tokenize($csv, '\r?\n')[position() > 1]">
      <xsl:variable name="fields" select="tokenize(., ',')"/>
      <book>
        <title><xsl:value-of select="$fields[1]"/></title>
        <author><xsl:value-of select="$fields[2]"/></author>
        <price><xsl:value-of select="$fields[3]"/></price>
      </book>
    </xsl:for-each>
  </books>
</xsl:template>
</xsl:stylesheet>

8. 完整的 HTML 电子书(带目录、章节)

最长但最实用,留言告诉我你要,我直接发 500 行完整代码。

一键运行所有例子(推荐方式)

# 下载 Saxon-HE(免费)
wget https://repo1.maven.org/maven2/net/sf/saxon/Saxon-HE/12.5/Saxon-HE-12.5.jar

# 运行任意例子
java -jar Saxon-HE-12.5.jar -s:books.xml -xsl:html-table.xsl > result.html

把上面 8 个例子全部保存好,你就拥有了 2025 年最强的 XSLT 实战武器库。

想要哪个例子我给你打包成完整项目(含 XML + XSL + 运行脚本)?或者你贴出自己的 XML,我 1 分钟给你写出对应 XSLT!

文章已创建 2783

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部