XSLT通过xsl:apply-templates的select属性实现节点的动态筛选,结合xsl:choose条件判断和mode模式切换,可在不同上下文中灵活选择模板,支持基于内容、属性或多视图需求的复杂转换,提升复用性与可维护性。

XSLT要动态选择模板应用,最直接的方式就是通过
xsl:apply-templates
select
xsl:choose
mode
在我看来,XSLT的动态模板选择,其实是它声明式和半命令式能力的一种巧妙结合。它不像传统编程语言那样有明确的
if-else
switch
首先,最基础的动态性体现在
xsl:apply-templates
<xsl:apply-templates/>
更进一步,使用
select
<xsl:apply-templates select="chapter/section[position() mod 2 = 1]"/>
但光靠
select
xsl:choose
xsl:when
xsl:otherwise
再往深了说,还有
mode
xsl:apply-templates
mode
xsl:apply-templates
常规的
xsl:apply-templates
select
<book>
<chapter>
<chapter>
<paragraph>
match="paragraph"
但问题来了,真实世界的数据往往没那么规整。设想一下,你有一个
<product>
status
<product>
xsl:if
xsl:choose
status
更头疼的是,有时候你需要对 同一个 XML源数据,生成 多份 完全不同格式或内容侧重点的输出。比如,一份是用于网页展示的详细产品页,另一份是用于打印的简要库存列表,再一份是给API用的JSON数据。如果只靠
xsl:apply-templates
所以,常规的匹配模式在面对复杂条件判断、多态处理或多视图转换时,就显得力不从心了。它缺乏那种根据“情境”来灵活调整处理逻辑的能力。
xsl:choose
xsl:choose
if-else if-else
xsl:choose
xsl:when
xsl:otherwise
xsl:when
test
xsl:when
xsl:when
xsl:otherwise
举个例子,假设我们有一个XML数据,描述了不同类型的消息:
<messages> <message type="info">系统升级通知。</message> <message type="warning">磁盘空间不足!</message> <message type="error">数据库连接失败。</message> <message type="debug">变量X的值是:123。</message> </messages>
我们希望根据
type
<xsl:template match="message">
<xsl:choose>
<xsl:when test="@type = 'info'">
<p class="info">ℹ️ <xsl:value-of select="."/></p>
</xsl:when>
<xsl:when test="@type = 'warning'">
<p class="warning">⚠️ <xsl:value-of select="."/></p>
</xsl:when>
<xsl:when test="@type = 'error'">
<p class="error">❌ <xsl:value-of select="."/></p>
</xsl:when>
<xsl:otherwise>
<!-- 默认处理,或者处理未预期的类型 -->
<p class="default"><xsl:value-of select="."/></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>在这个例子中,当XSLT处理器匹配到
<message>
xsl:choose
@type
@type
<p>
type
xsl:when
xsl:otherwise
这种方式的优势在于,它将条件判断逻辑集中在一个地方,使得模板的意图非常清晰。你可以根据数据的内在特性,动态地选择生成哪种结构或内容,而不是为每一种可能性都写一个独立的模板。这对于处理具有多态性质的数据结构特别有用。
mode
mode
<xsl:template match="someNode">
mode
xsl:apply-templates
mode
在我看来,
mode
如果不用
mode
xsl:if
xsl:choose
有了
mode
<!-- XML源数据示例 -->
<!--
<book>
<title>XSLT深度指南</title>
<author>张三</author>
<chapter id="ch1">
<title>XSLT简介</title>
<paragraph>...</paragraph>
</chapter>
<chapter id="ch2">
<title>模板与匹配</title>
<paragraph>...</paragraph>
</chapter>
</book>
-->
<!-- 模式1:生成图书列表摘要 -->
<xsl:template match="book" mode="list-summary">
<h2>图书列表</h2>
<ul>
<li>
<strong><xsl:value-of select="title"/></strong> by <xsl:value-of select="author"/>
</li>
</ul>
</xsl:template>
<!-- 模式2:生成详细图书页面 -->
<xsl:template match="book" mode="detail-view">
<h1><xsl:value-of select="title"/></h1>
<p>作者:<xsl:value-of select="author"/></p>
<div class="chapters">
<xsl:apply-templates select="chapter" mode="detail-view"/>
</div>
</xsl:template>
<xsl:template match="chapter" mode="detail-view">
<h3><xsl:value-of select="title"/></h3>
<xsl:apply-templates select="paragraph"/> <!-- 段落默认模式处理 -->
</xsl:template>
<!-- 模式3:生成目录 -->
<xsl:template match="book" mode="toc">
<h2>目录</h2>
<ul>
<xsl:apply-templates select="chapter" mode="toc"/>
</ul>
</xsl:template>
<xsl:template match="chapter" mode="toc">
<li><a href="#{@id}"><xsl:value-of select="title"/></a></li>
</template>
<!-- 如何调用不同模式: -->
<!-- 假设你在一个主模板中,或者通过XSLT处理器参数来控制 -->
<!-- 要生成列表摘要: -->
<!-- <xsl:apply-templates select="/book" mode="list-summary"/> -->
<!-- 要生成详细页面: -->
<!-- <xsl:apply-templates select="/book" mode="detail-view"/> -->
<!-- 要生成目录: -->
<!-- <xsl:apply-templates select="/book" mode="toc"/> -->在这个例子中,
<book>
<chapter>
mode
xsl:apply-templates
mode
以上就是XSLT如何动态选择模板应用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号