XSLT <xsl:if> 元素最全详解(2025 年实战版,一篇吃透所有用法)
<xsl:if> 是 XSLT 中最简单最直接的条件判断,只有“满足就执行”,没有 else。
一、基本语法(永远就这 1 种写法)
<xsl:if test="布尔表达式">
<!-- 满足条件时执行这里的代码 -->
</xsl:if>
二、核心属性(只有 1 个)
| 属性 | 是否必须 | 说明 | 举例 |
|---|---|---|---|
test | 必须 | 布尔类型的 XPath 表达式 | test="price > 50"、test="@vip"、test="contains(title,'三体')" |
三、2025 年最常用的 15 种真实写法(直接复制就可用)
<!-- 1. 价格大于 50 显示“贵” -->
<xsl:if test="price > 50">
<span style="color:red">贵</span>
</xsl:if>
<!-- 2. 有折扣才显示原价并划掉 -->
<xsl:if test="discount">
<del>¥<xsl:value-of select="original-price"/></del>
</xsl:if>
<!-- 3. 是推荐书加星标 -->
<xsl:if test="@recommend = 'true'">
<xsl:text> ★ </xsl:text>
</xsl:if>
<!-- 4. 库存为 0 显示“售罄” -->
<xsl:if test="stock = 0">
<span class="soldout">售罄</span>
</xsl:if>
<!-- 5. 奇数行加背景色(经典表格技巧) -->
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="bgcolor">#f9f9f9</xsl:attribute>
</xsl:if>
<!-- 6. 最后一行不输出逗号(生成 JSON/CSV 神技) -->
<xsl:value-of select="title"/>
<xsl:if test="position() != last()">, </xsl:if>
<!-- 7. 包含关键字高亮 -->
<xsl:if test="contains(title, '三体')">
<span class="highlight"><xsl:value-of select="title"/></span>
</xsl:if>
<!-- 8. 有封面图才输出 img 标签 -->
<xsl:if test="cover">
<img src="{cover}" alt="{title}"/>
</xsl:if>
<!-- 9. 评分大于等于 9 显示“神作”标签 -->
<xsl:if test="rating >= 9">
<span class="badge god">神作</span>
</xsl:if>
<!-- 10. 属性存在才输出 -->
<xsl:if test="@isbn">
ISBN: <xsl:value-of select="@isbn"/><br/>
</xsl:if>
<!-- 11. 字符串长度大于 20 截断 -->
<xsl:if test="string-length(description) > 20">
<xsl:value-of select="substring(description, 1, 20)"/>...
</xsl:if>
<!-- 12. 同时满足多个条件(用 and) -->
<xsl:if test="price < 30 and stock > 0">
<button>23>特价秒杀</span>
</xsl:if>
<!-- 13. 满足任一条件(用 or) -->
<xsl:if test="contains(title,'三体') or contains(author,'刘慈欣')">
<span class="tag">科幻</span>
</xsl:if>
<!-- 14. 否定条件(用 not()) -->
<xsl:if test="not(@hidden)">
<xsl:apply-templates/>
</xsl:if>
<!-- 15. 经典:第一条数据前不输出分隔线 -->
<xsl:if test="position() != 1">
<hr/>
</xsl:if>
四、<xsl:if> vs <xsl:choose>(终极对比表)
| 项目 | <xsl:if> | <xsl:choose>(推荐用于多条件) |
|---|---|---|
| 是否有 else | 没有 | 有(<xsl:otherwise>) |
| 多条件可读性 | 差(容易写出嵌套地狱) | 优秀(像 switch-case) |
| 性能 | 略快 | 基本一样 |
| 推荐使用场景 | 简单单一条件 | 有 else 或 3 个以上分支 |
| 2025 年老手使用比例 | 40% | 60% |
结论:
能用 <xsl:choose> 就别用多个 <xsl:if> 嵌套!
五、经典错误写法 vs 正确写法(99% 人都踩过)
| 想实现的效果 | 错误写法(会出错或不优雅) | 正确/推荐写法 |
|---|---|---|
| 有 else 逻辑 | 两个 <xsl:if> 反着写 | 用 <xsl:choose><xsl:when><xsl:otherwise> |
| 价格 < 30 显示“便宜” | <xsl:if test="price < 30">便宜</xsl:if> | 直接写(上面是对的) |
| 同时判断多个条件 | 嵌套一堆 if | 用 and / or 或 <xsl:choose> |
| 想在标签上加 class | <xsl:if test="..."><div class="hot"></div></xsl:if> | 用 <xsl:attribute> 或直接写属性 |
六、30 秒实战挑战(2025 年最常见需求)
把下面 XML 变成:
《三体》 ¥69.00 ★神作 科幻(特价)
《活着》 ¥28.00 经典(售罄)
<books>
<book hot="true">
<title>三体</title><price>69.00</price><rating>9.5</rating><tag>科幻</tag><discount>0.8</discount>
</book>
<book>
<title>活着</title><price>28.00</price><stock>0</stock>
</book>
</books>
答案(只用 <xsl:if> 就能完美实现):
<xsl:for-each select="books/book">
《<xsl:value-of select="title"/>》 ¥<xsl:value-of select="price"/>
<xsl:if test="rating >= 9"> ★神作</xsl:if>
<xsl:if test="tag"> <xsl:value-of select="tag"/></xsl:if>
<xsl:if test="discount">(特价)</xsl:if>
<xsl:if test="stock = 0">(售罄)</xsl:if>
<br/>
</xsl:for-each>
七、一句话总结
<xsl:if test="条件">就是 XSLT 里的if (条件) { … }没有 else
简单条件用它爽得飞起,复杂条件赶紧上<xsl:choose>
需要我现在用 <xsl:if>(或结合 <xsl:choose>)帮你实现任何效果?
- 高亮特价书
- 库存预警
- VIP 专享内容
- 评分星级显示
- 分类标签自动打
直接甩 XML + 需求,10 秒出成品!