XSLT调用模板主要有xsl:apply-templates和xsl:call-template两种方式:前者基于匹配规则自动处理节点,实现数据驱动的递归遍历;后者通过名称直接调用模板,支持参数传递,适用于过程式复用。两者结合可高效构建结构清晰、可维护的转换逻辑。

XSLT调用模板主要通过两种核心机制实现:
xsl:apply-templates
xsl:call-template
在XSLT中,模板调用是其转换逻辑的基石,可以说,没有模板调用,XSLT就失去了灵魂。我们通常有两种主要方式来“激活”这些模板:
1. xsl:apply-templates
这是XSLT最强大也最常用的模板调用方式。它不是直接指定一个模板的名称,而是告诉XSLT处理器:“去处理当前节点集中的所有子节点(或者你通过
select
它的工作机制有点像一个智能的调度员:
<xsl:apply-templates/>
<xsl:apply-templates select="某个XPath表达式"/>
<xsl:template match="..."/>
我个人觉得,
xsl:apply-templates
示例:
<xsl:template match="/">
  <html>
    <body>
      <h1>书籍列表</h1>
      <xsl:apply-templates select="catalog/book"/> <!-- 处理所有book节点 -->
    </body>
  </html>
</xsl:template>
<xsl:template match="book">
  <div>
    <h2><xsl:value-of select="title"/></h2>
    <p>作者: <xsl:value-of select="author"/></p>
    <xsl:apply-templates select="price"/> <!-- 处理book下的price节点 -->
  </div>
</xsl:template>
<xsl:template match="price">
  <p>价格: <xsl:value-of select="."/></p>
</xsl:template>2. xsl:call-template
与
xsl:apply-templates
xsl:call-template
name
match
我通常把
xsl:call-template
示例:
<xsl:template match="/">
  <html>
    <body>
      <xsl:call-template name="header"/> <!-- 调用名为header的模板 -->
      <p>这里是主内容。</p>
      <xsl:call-template name="footer"/> <!-- 调用名为footer的模板 -->
    </body>
  </html>
</xsl:template>
<xsl:template name="header">
  <div class="header">
    <h1>我的网站</h1>
  </div>
</xsl:template>
<xsl:template name="footer">
  <div class="footer">
    <p>© 2023 版权所有</p>
  </div>
</xsl:template>apply-templates
call-template
这个问题是XSLT初学者经常会遇到的一个“哲学”问题,理解它们之间的差异对于写出高效且可维护的XSLT至关重要。
核心区别在于它们的触发机制和上下文处理方式:
xsl:apply-templates
select
<xsl:template match="..."/>
apply-templates
.
xsl:with-param
call-template
xsl:call-template
name
<xsl:template name="..."/>
match
call-template
call-template
xsl:param
call-template
xsl:with-param
apply-templates
call-template
我个人经验总结: 如果你的目标是遍历和转换XML文档的结构,让XSLT引擎自己决定如何处理每个节点,那么
xsl:apply-templates
xsl:call-template
apply-templates
call-template
在XSLT中,参数传递是实现模板复用性和灵活性的关键。无论是
xsl:apply-templates
xsl:call-template
xsl:with-param
1. 定义参数:xsl:param
首先,在你要接收参数的模板内部,你需要使用
xsl:param
xsl:param
xsl:template
name
select
xsl:param
示例:
<xsl:template name="formatText">
  <xsl:param name="textToFormat" select="''"/> <!-- 定义一个字符串参数,默认值为空字符串 -->
  <xsl:param name="isBold" select="false()"/> <!-- 定义一个布尔参数,默认值为false -->
  <xsl:choose>
    <xsl:when test="$isBold">
      <b><xsl:value-of select="$textToFormat"/></b>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$textToFormat"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>2. 传递参数:xsl:with-param
当你调用模板时(无论是通过
xsl:apply-templates
xsl:call-template
xsl:with-param
name
xsl:param
name
select
xsl:with-param
示例 (结合xsl:call-template
<xsl:template match="/">
  <root>
    <!-- 调用formatText模板,传递两个参数 -->
    <output1>
      <xsl:call-template name="formatText">
        <xsl:with-param name="textToFormat" select="'这是加粗的文本'"/>
        <xsl:with-param name="isBold" select="true()"/>
      </xsl:call-template>
    </output1>
    <!-- 再次调用,只传递一个参数,另一个使用默认值 -->
    <output2>
      <xsl:call-template name="formatText">
        <xsl:with-param name="textToFormat" select="'这是普通文本'"/>
      </xsl:call-template>
    </output2>
    <!-- 甚至可以从XML数据中获取参数值 -->
    <xsl:call-template name="formatText">
        <xsl:with-param name="textToFormat" select="/data/item[1]/text()"/>
        <xsl:with-param name="isBold" select="/data/item[1]/@bold = 'true'"/>
    </xsl:call-template>
  </root>
</xsl:template>示例 (结合xsl:apply-templates
虽然
apply-templates
<xsl:template match="document">
  <xsl:apply-templates select="section">
    <xsl:with-param name="documentTitle" select="title"/> <!-- 将document的title传递给section模板 -->
  </xsl:apply-templates>
</xsl:template>
<xsl:template match="section">
  <xsl:param name="documentTitle"/> <!-- 接收documentTitle参数 -->
  <h2><xsl:value-of select="$documentTitle"/> - <xsl:value-of select="title"/></h2>
  <xsl:apply-templates/>
</xsl:template>需要注意的几点:
xsl:param
xsl:with-param
xsl:param
xsl:stylesheet
我发现,合理使用参数可以极大地提高XSLT代码的模块化和可维护性,避免了重复代码,并使得模板能够处理更广泛的输入场景。
随着XSLT项目的规模增长,模板数量会迅速增加,如果管理不当,很容易变得混乱和难以维护。有效的组织和管理策略至关重要。我个人在处理大型XSLT项目时,通常会从以下几个方面入手:
1. 模块化:拆分XSLT文件
这是最基本也是最重要的策略。不要把所有模板都塞到一个巨大的XSLT文件中。根据功能或XML结构,将相关的模板分组到单独的
.xsl
xsl:include
xsl:include
#include
xsl:import
xsl:import
xsl:include
xsl:import
xsl:import
xsl:stylesheet
xsl:template
xsl:variable
我个人建议:
xsl:include
xsl:import
2. 命名规范:清晰且一致
为模板、参数和变量使用一套清晰、一致的命名规范,这对于团队协作和长期维护至关重要。
formatDate
renderProductCard
generateHeader
currentPrice
isDiscounted
3. 默认模板与模式匹配:利用XSLT的声明性
不要过度使用
xsl:call-template
xsl:template match="...
mode
4. 错误处理与调试
在复杂转换中,错误是不可避免的。
xsl:message
5. 注释:适度但有价值
清晰的注释可以帮助你和团队成员理解复杂逻辑。
通过这些策略的组合使用,即使面对极其复杂的XML转换需求,我也能保持XSLT代码的清晰、可维护和可扩展性。关键在于将大问题拆解成小问题,并充分利用XSLT语言本身的特性来解决它们。
以上就是XSLT如何调用模板?的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号