☰
XSLT 函数详解:从基础到高级,附丰富代码实例
2026/10/17 19:08:14 网站建设 项目流程

1. XSLT 函数概述

XSLT(Extensible Stylesheet Language Transformations)是一种用于将 XML 文档转换为其他格式(如 HTML、XML、文本)的语言。XSLT 函数是 XSLT 处理过程中的核心工具,它们提供了强大的数据处理能力,包括字符串操作、数值计算、日期处理、节点集操作等。本文将深入探讨 XSLT 内置函数、自定义函数的使用,并通过丰富的代码实例展示其实际应用。

2. 核心内置函数分类与实例

2.1 字符串函数

XSLT 提供了丰富的字符串处理函数,以下是一些常用函数的示例:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1>字符串函数示例</h1> <p>原始字符串: <xsl:value-of select="'Hello XSLT World'" /></p> <p>转换为大写: <xsl:value-of select="translate('Hello XSLT World', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /></p> <p>字符串长度: <xsl:value-of select="string-length('Hello XSLT World')" /></p> <p>子字符串(从第7位开始取4个字符): <xsl:value-of select="substring('Hello XSLT World', 7, 4)" /></p> <p>替换'World'为'Functions': <xsl:value-of select="concat(substring-before('Hello XSLT World', 'World'), 'Functions')" /></p> </body> </html> </xsl:template> </xsl:stylesheet>

2.2 数值函数

数值函数用于数学计算和数值处理:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1>数值函数示例</h1> <p>绝对值(-15.5): <xsl:value-of select="abs(-15.5)" /></p> <p>四舍五入(3.14159, 2): <xsl:value-of select="round(3.14159 * 100) div 100" /></p> <p>向下取整(7.8): <xsl:value-of select="floor(7.8)" /></p> <p>向上取整(7.2): <xsl:value-of select="ceiling(7.2)" /></p> <p>随机数(1-100): <xsl:value-of select="floor((100 - 1 + 1) * math:random()) + 1" xmlns:math="http://exslt.org/math" /></p> <p>求和(1,2,3,4,5): <xsl:value-of select="sum((1,2,3,4,5))" /></p> </body> </html> </xsl:template> </xsl:stylesheet>

2.3 节点集函数

节点集函数用于处理 XML 节点集合:

<?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1>节点集函数示例</h1> <xsl:variable name="books" select="//book" /> <p>书籍总数: <xsl:value-of select="count($books)" /></p> <p>第一本书标题: <xsl:value-of select="$books[1]/title" /></p> <p>最后一本书标题: <xsl:value-of select="$books[last()]/title" /></p> <p>位置大于2的书籍数量: <xsl:value-of select="count($books[position() > 2])" /></p> <p>所有价格的总和: <xsl:value-of select="sum($books/price)" /></p> </body> </html> </xsl:template> </xsl:stylesheet>

2.4 日期和时间函数

XSLT 2.0 及更高版本提供了强大的日期时间处理功能:

<?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/"> <html> <body> <h1>日期时间函数示例</h1> <p>当前日期: <xsl:value-of select="current-date()" /></p> <p>当前时间: <xsl:value-of select="current-time()" /></p> <p>当前日期时间: <xsl:value-of select="current-dateTime()" /></p> <p>日期格式化: <xsl:value-of select="format-date(current-date(), '[D01]/[M01]/[Y0001]')" /></p> <p>日期加减(加10天): <xsl:value-of select="current-date() + xs:dayTimeDuration('P10D')" /></p> <p>两个日期的天数差: <xsl:value-of select="days-from-duration(xs:date('2024-12-31') - xs:date('2024-01-01'))" /></p> </body> </html> </xsl:template> </xsl:stylesheet>

3. 自定义函数(XSLT 2.0+)

XSLT 2.0 引入了强大的自定义函数功能,使用<xsl:function>元素定义:

3.1 基本自定义函数

<?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://example.com/my-functions"> <!-- 定义自定义函数:计算阶乘 --> <xsl:function name="my:factorial" as="xs:integer"> <xsl:param name="n" as="xs:integer"/> <xsl:choose> <xsl:when test="$n <= 1"> <xsl:sequence select="1"/> </xsl:when> <xsl:otherwise> <xsl:sequence select="$n * my:factorial($n - 1)"/> </xsl:otherwise> </xsl:choose> </xsl:function> <!-- 定义自定义函数:格式化货币 --> <xsl:function name="my:format-currency" as="xs:string"> <xsl:param name="amount" as="xs:decimal"/> <xsl:param name="currency" as="xs:string"/> <xsl:sequence select="concat($currency, format-number($amount, '#,##0.00'))"/> </xsl:function> <xsl:template match="/"> <html> <body> <h1>自定义函数示例</h1> <p>5的阶乘: <xsl:value-of select="my:factorial(5)"/></p> <p>格式化货币: <xsl:value-of select="my:format-currency(1234.56, '$')"/></p> </body> </html> </xsl:template> </xsl:stylesheet>

3.2 处理节点集的自定义函数

<?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://example.com/my-functions"> <!-- 定义函数:查找最贵的商品 --> <xsl:function name="my:find-most-expensive" as="element()?"> <xsl:param name="products" as="element()*"/> <xsl:sequence select="$products[not(price < ../price)][1]"/> </xsl:function> <!-- 定义函数:计算平均值 --> <xsl:function name="my:average-price" as="xs:decimal?"> <xsl:param name="products" as="element()*"/> <xsl:if test="exists($products)"> <xsl:sequence select="avg($products/price)"/> </xsl:if> </xsl:function> <xsl:template match="/"> <html> <body> <h1>节点集处理函数示例</h1> <xsl:variable name="products" select="//product"/> <p>最贵商品: <xsl:value-of select="my:find-most-expensive($products)/name"/></p> <p>平均价格: <xsl:value-of select="my:average-price($products)"/></p> </body> </html> </xsl:template> </xsl:stylesheet>

4. 高级函数应用实例

4.1 递归函数处理层次结构

<?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://example.com/my-functions"> <!-- 递归函数:生成目录树 --> <xsl:function name="my:generate-toc" as="element(ul)"> <xsl:param name="nodes" as="element()*"/> <xsl:param name="level" as="xs:integer"/> <ul> <xsl:for-each select="$nodes"> <li> <xsl:value-of select="@title"/> <xsl:if test="section"> <xsl:sequence select="my:generate-toc(section, $level + 1)"/> </xsl:if> </li> </xsl:for-each> </ul> </xsl:function> <xsl:template match="/"> <html> <body> <h1>文档目录</h1> <xsl:sequence select="my:generate-toc(//chapter, 1)"/> </body> </html> </xsl:template> </xsl:stylesheet>

4.2 函数组合与管道处理

<?xml version="1.0"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://example.com/my-functions" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <!-- 字符串处理管道函数 --> <xsl:function name="my:process-text" as="xs:string"> <xsl:param name="input" as="xs:string"/> <xsl:sequence select=" $input => fn:normalize-space() => fn:upper-case() => fn:replace('[^A-Z0-9\s]', '') => fn:substring(1, 50) "/> </xsl:function> <!-- 数据转换管道 --> <xsl:function name="my:transform-data" as="element(result)"> <xsl:param name="data" as="element()*"/> <result> <xsl:for-each select="$data"> <item> <name><xsl:value-of select="my:process-text(name)"/></name> <price><xsl:value-of select="format-number(price * 1.1, '#0.00')"/></price> <category><xsl:value-of select="upper-case(category)"/></category> </item> </xsl:for-each> </result> </xsl:function> <xsl:template match="/"> <html> <body> <h1>函数组合与管道处理</h1> <xsl:sequence select="my:transform-data(//product)"/> </body> </html> </xsl:template> </xsl:stylesheet>

5. 性能优化与最佳实践

5.1 函数缓存与记忆化

<?xml version="1.0"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://example.com/my-functions" xmlns:map="http://www.w3.org/2005/xpath-functions/map"> <!-- 带缓存的斐波那契函数 --> <xsl:variable name="fib-cache" select="map{}" as="map(xs:integer, xs:integer)"/> <xsl:function name="my:fibonacci" as="xs:integer"> <xsl:param name="n" as="xs:integer"/> <xsl:choose> <xsl:when test="$n <= 1"> <xsl:sequence select="$n"/> </xsl:when> <xsl:when test="map:contains($fib-cache, $n)"> <xsl:sequence select="$fib-cache($n)"/> </xsl:when> <xsl:otherwise> <xsl:variable name="result" select="my:fibonacci($n - 1) + my:fibonacci($n - 2)"/> <xsl:variable name="new-cache" select="map:put($fib-cache, $n, $result)"/> <xsl:sequence select="$result"/> </xsl:otherwise> </xsl:choose> </xsl:function> <xsl:template match="/"> <html> <body> <h1>带缓存的斐波那契计算</h1> <p>斐波那契(10)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询