ARTICLE DETAIL

资讯详情

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

XSLT 函数详解:从基础到高级,附丰富代码实例

XSLT 函数详解:从基础到高级,附丰富代码实例 1. XSLT 函数概述XSLTExtensible Stylesheet Language Transformations是一种用于将 XML 文档转换为其他格式如 HTML、XML、文本的语言。XSLT 函数是 XSLT 处理过程中的核心工具它们提供了强大的数据处理能力包括字符串操作、数值计算、日期处理、节点集操作等。本文将深入探讨 XSLT 内置函数、自定义函数的使用并通过丰富的代码实例展示其实际应用。2. 核心内置函数分类与实例2.1 字符串函数XSLT 提供了丰富的字符串处理函数以下是一些常用函数的示例?xml version1.0? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html body h1字符串函数示例/h1 p原始字符串: xsl:value-of selectHello XSLT World //p p转换为大写: xsl:value-of selecttranslate(Hello XSLT World, abcdefghijklmnopqrstuvwxyz, ABCDEFGHIJKLMNOPQRSTUVWXYZ) //p p字符串长度: xsl:value-of selectstring-length(Hello XSLT World) //p p子字符串(从第7位开始取4个字符): xsl:value-of selectsubstring(Hello XSLT World, 7, 4) //p p替换World为Functions: xsl:value-of selectconcat(substring-before(Hello XSLT World, World), Functions) //p /body /html /xsl:template /xsl:stylesheet2.2 数值函数数值函数用于数学计算和数值处理?xml version1.0? xsl:stylesheet version1.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html body h1数值函数示例/h1 p绝对值(-15.5): xsl:value-of selectabs(-15.5) //p p四舍五入(3.14159, 2): xsl:value-of selectround(3.14159 * 100) div 100 //p p向下取整(7.8): xsl:value-of selectfloor(7.8) //p p向上取整(7.2): xsl:value-of selectceiling(7.2) //p p随机数(1-100): xsl:value-of selectfloor((100 - 1 1) * math:random()) 1 xmlns:mathhttp://exslt.org/math //p p求和(1,2,3,4,5): xsl:value-of selectsum((1,2,3,4,5)) //p /body /html /xsl:template /xsl:stylesheet2.3 节点集函数节点集函数用于处理 XML 节点集合?xml version1.0? xsl:stylesheet version2.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xsl:template match/ html body h1节点集函数示例/h1 xsl:variable namebooks select//book / p书籍总数: xsl:value-of selectcount($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 selectcount($books[position() 2]) //p p所有价格的总和: xsl:value-of selectsum($books/price) //p /body /html /xsl:template /xsl:stylesheet2.4 日期和时间函数XSLT 2.0 及更高版本提供了强大的日期时间处理功能?xml version1.0? xsl:stylesheet version2.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:xshttp://www.w3.org/2001/XMLSchema xsl:template match/ html body h1日期时间函数示例/h1 p当前日期: xsl:value-of selectcurrent-date() //p p当前时间: xsl:value-of selectcurrent-time() //p p当前日期时间: xsl:value-of selectcurrent-dateTime() //p p日期格式化: xsl:value-of selectformat-date(current-date(), [D01]/[M01]/[Y0001]) //p p日期加减(加10天): xsl:value-of selectcurrent-date() xs:dayTimeDuration(P10D) //p p两个日期的天数差: xsl:value-of selectdays-from-duration(xs:date(2024-12-31) - xs:date(2024-01-01)) //p /body /html /xsl:template /xsl:stylesheet3. 自定义函数XSLT 2.0XSLT 2.0 引入了强大的自定义函数功能使用xsl:function元素定义3.1 基本自定义函数?xml version1.0? xsl:stylesheet version2.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:myhttp://example.com/my-functions !-- 定义自定义函数计算阶乘 -- xsl:function namemy:factorial asxs:integer xsl:param namen asxs:integer/ xsl:choose xsl:when test$n 1 xsl:sequence select1/ /xsl:when xsl:otherwise xsl:sequence select$n * my:factorial($n - 1)/ /xsl:otherwise /xsl:choose /xsl:function !-- 定义自定义函数格式化货币 -- xsl:function namemy:format-currency asxs:string xsl:param nameamount asxs:decimal/ xsl:param namecurrency asxs:string/ xsl:sequence selectconcat($currency, format-number($amount, #,##0.00))/ /xsl:function xsl:template match/ html body h1自定义函数示例/h1 p5的阶乘: xsl:value-of selectmy:factorial(5)//p p格式化货币: xsl:value-of selectmy:format-currency(1234.56, $)//p /body /html /xsl:template /xsl:stylesheet3.2 处理节点集的自定义函数?xml version1.0? xsl:stylesheet version2.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:myhttp://example.com/my-functions !-- 定义函数查找最贵的商品 -- xsl:function namemy:find-most-expensive aselement()? xsl:param nameproducts aselement()*/ xsl:sequence select$products[not(price ../price)][1]/ /xsl:function !-- 定义函数计算平均值 -- xsl:function namemy:average-price asxs:decimal? xsl:param nameproducts aselement()*/ xsl:if testexists($products) xsl:sequence selectavg($products/price)/ /xsl:if /xsl:function xsl:template match/ html body h1节点集处理函数示例/h1 xsl:variable nameproducts select//product/ p最贵商品: xsl:value-of selectmy:find-most-expensive($products)/name//p p平均价格: xsl:value-of selectmy:average-price($products)//p /body /html /xsl:template /xsl:stylesheet4. 高级函数应用实例4.1 递归函数处理层次结构?xml version1.0? xsl:stylesheet version2.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:myhttp://example.com/my-functions !-- 递归函数生成目录树 -- xsl:function namemy:generate-toc aselement(ul) xsl:param namenodes aselement()*/ xsl:param namelevel asxs:integer/ ul xsl:for-each select$nodes li xsl:value-of selecttitle/ xsl:if testsection xsl:sequence selectmy:generate-toc(section, $level 1)/ /xsl:if /li /xsl:for-each /ul /xsl:function xsl:template match/ html body h1文档目录/h1 xsl:sequence selectmy:generate-toc(//chapter, 1)/ /body /html /xsl:template /xsl:stylesheet4.2 函数组合与管道处理?xml version1.0? xsl:stylesheet version3.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:myhttp://example.com/my-functions xmlns:fnhttp://www.w3.org/2005/xpath-functions !-- 字符串处理管道函数 -- xsl:function namemy:process-text asxs:string xsl:param nameinput asxs: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 namemy:transform-data aselement(result) xsl:param namedata aselement()*/ result xsl:for-each select$data item namexsl:value-of selectmy:process-text(name)//name pricexsl:value-of selectformat-number(price * 1.1, #0.00)//price categoryxsl:value-of selectupper-case(category)//category /item /xsl:for-each /result /xsl:function xsl:template match/ html body h1函数组合与管道处理/h1 xsl:sequence selectmy:transform-data(//product)/ /body /html /xsl:template /xsl:stylesheet5. 性能优化与最佳实践5.1 函数缓存与记忆化?xml version1.0? xsl:stylesheet version3.0 xmlns:xslhttp://www.w3.org/1999/XSL/Transform xmlns:myhttp://example.com/my-functions xmlns:maphttp://www.w3.org/2005/xpath-functions/map !-- 带缓存的斐波那契函数 -- xsl:variable namefib-cache selectmap{} asmap(xs:integer, xs:integer)/ xsl:function namemy:fibonacci asxs:integer xsl:param namen asxs:integer/ xsl:choose xsl:when test$n 1 xsl:sequence select$n/ /xsl:when xsl:when testmap:contains($fib-cache, $n) xsl:sequence select$fib-cache($n)/ /xsl:when xsl:otherwise xsl:variable nameresult selectmy:fibonacci($n - 1) my:fibonacci($n - 2)/ xsl:variable namenew-cache selectmap:put($fib-cache, $n, $result)/ xsl:sequence select$result/ /xsl:otherwise /xsl:choose /xsl:function xsl:template match/ html body h1带缓存的斐波那契计算/h1 p斐波那契(10)
返回列表