XSLT中的条件处理通过xsl:if和xsl:choose结合XPath实现,xsl:if适用于单一条件判断,xsl:choose用于多重互斥条件及默认情况处理,XPath提供强大的表达式支持,如逻辑运算、函数和轴,确保转换的灵活性和精准性。

XSLT中的条件处理,说白了,就是让你能根据XML文档中数据的不同情况,输出不同的内容或执行不同的转换逻辑。这和我们日常编程语言里的
if/else
switch/case
xsl:if
xsl:choose
在XSLT中,处理条件逻辑主要围绕
xsl:if
xsl:choose
1. xsl:if
xsl:if
test
true
xsl:if
if
else
示例: 假设我们有一个产品列表,只想显示库存大于0的产品。
XML (products.xml):
<products> <product id="p1" name="Laptop" stock="10"/> <product id="p2" name="Mouse" stock="0"/> <product id="p3" name="Keyboard" stock="5"/> </products>
XSLT (transform.xsl):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/products">
<html>
<body>
<h1>Available Products</h1>
<ul>
<xsl:apply-templates select="product"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<xsl:if test="@stock > 0">
<li>
<xsl:value-of select="@name"/> (Stock: <xsl:value-of select="@stock"/>)
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>在这个例子里,只有当
product
stock
<li>
2. xsl:choose
当你的逻辑需要处理多个互斥的条件,并且可能包含一个默认的“否则”情况时,
xsl:choose
if-else if-else
switch-case-default
xsl:choose
xsl:when
xsl:otherwise
xsl:when
xsl:when
test
test
true
xsl:when
xsl:choose
xsl:otherwise
xsl:when
false
xsl:otherwise
xsl:choose
示例: 根据产品的状态显示不同的信息。
XML (products.xml):
<products> <product id="p1" name="Laptop" status="available"/> <product id="p2" name="Mouse" status="out-of-stock"/> <product id="p3" name="Keyboard" status="preorder"/> <product id="p4" name="Monitor" status="discontinued"/> </products>
XSLT (transform.xsl):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/products">
<html>
<body>
<h1>Product Status</h1>
<ul>
<xsl:apply-templates select="product"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<li>
<xsl:value-of select="@name"/>:
<xsl:choose>
<xsl:when test="@status = 'available'">
<span style="color: green;">In Stock!</span>
</xsl:when>
<xsl:when test="@status = 'out-of-stock'">
<span style="color: red;">Currently Unavailable.</span>
</xsl:when>
<xsl:when test="@status = 'preorder'">
<span style="color: blue;">Pre-order Now!</span>
</xsl:when>
<xsl:otherwise>
<span>Status Unknown. Please check back later.</span>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:template>
</xsl:stylesheet>这个例子展示了如何根据
product
status
XPath在test
无论是
xsl:if
xsl:when
test
true
false
test="@price > 100"
test="count(./description) > 0"
test="@id"
true
test="(@category = 'electronics' and @stock > 0)"
test="starts-with(@name, 'New')"
test="contains(./tags, 'urgent')"
正是XPath的灵活性,让XSLT的条件处理变得异常强大和精准。
这其实是一个很实际的问题,我在写XSLT时也经常琢磨。说白了,选择哪个工具,主要看你的条件逻辑有多复杂,以及你是否需要一个“万一都不满足”的默认处理。
选择 xsl:if
xsl:if
<div>
else
xsl:if
xsl:if
xsl:if
xsl:choose
选择 xsl:choose
xsl:choose
xsl:choose
xsl:if
xsl:choose
xsl:otherwise
xsl:otherwise
xsl:choose
xsl:if
我的个人经验:
我通常会这样思考:如果我能用一个
if
xsl:if
if
xsl:choose
xsl:otherwise
XPath在XSLT的条件判断中,简直就是灵魂所在。
test
1. 组合逻辑:and
or
not()
这是最基础也最常用的复杂逻辑构建方式。
and
test="(@status = 'active' and @category = 'premium')"
or
test="(@type = 'admin' or @type = 'moderator')"
not()
test="not(@visible = 'false')"
test="@visible != 'false'"
test="not(./comment)"
comment
2. 强大的XPath函数:
XPath提供了大量的内置函数,可以用于字符串处理、数值计算、节点集操作等,这些在条件判断中非常有用。
starts-with(string, prefix)
test="starts-with(@name, 'Special')"
contains(string, substring)
test="contains(./description, 'urgent')"
ends-with(string, suffix)
string-length(string)
test="string-length(@code) = 5"
normalize-space(string)
concat(string1, string2, ...)
contains
test="contains('|red|green|blue|', concat('|', @color, '|'))"number(value)
test="number(@price) > 100.00"
sum(node-set)
test="sum(./item/price) > 500"
count(node-set)
test="count(./user) > 10"
position()
test="position() = 1"
last()
test="position() = last()"
local-name(node)
test="local-name() = 'product'"
name(node)
true()
false()
3. 轴(Axes)的运用:
虽然
test
parent::node-name
test="parent::section/@type = 'important'"
ancestor::node-name
following-sibling::node-name
preceding-sibling::node-name
descendant::node-name
示例: 检查当前商品是否是其父类别中的最后一个,并且该类别是“特价”类别。
test="position() = last() and ancestor::category/@name = 'SpecialOffers'"
我的看法:
XPath的强大在于它的表达能力。很多时候,我们觉得一个条件很复杂,需要多步处理,但用好XPath函数和轴,往往能把几步逻辑浓缩到一个
test
XSLT的条件处理虽然强大,但使用不当也容易踩坑,或者写出效率不高的代码。这些都是我在实际项目中遇到过的一些问题和总结出的经验。
常见的陷阱:
if/else
if (condition) { true_value } else { false_value }xsl:choose
以上就是XSLT如何条件处理?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号