XSLT <xsl:choose> 元素最全详解(2025 年实战版,一篇彻底吃透)
<xsl:choose> 是 XSLT 中唯一真正的多分支条件语句,相当于 JavaScript/Python 里的 if … else if … else,写法超级清晰。
一、完整语法(永远就这 3 个标签)
<xsl:choose>
<xsl:when test="条件1">
<!-- 满足条件1时执行 -->
</xsl:when>
<xsl:when test="条件2">
<!-- 满足条件2时执行(条件1不满足的前提下) -->
</xsl:when>
<!-- 可以有 N 个 when -->
<xsl:otherwise>
<!-- 上面所有条件都不满足时执行(相当于 else) -->
</xsl:otherwise>
</xsl:choose>
二、2025 年最常用的 12 种真实场景写法(直接复制就可用)
<!-- 1. 最经典:价格区间判断(所有商城必备) -->
<xsl:choose>
<xsl:when test="price < 30">超值</xsl:when>
<xsl:when test="price < 60">特价</xsl:when>
<xsl:when test="price < 100">正常价</xsl:when>
<xsl:otherwise>高端大气</xsl:otherwise>
</xsl:choose>
<!-- 2. 评分星级显示 -->
<xsl:choose>
<xsl:when test="rating >= 9.5">5星</xsl:when>
<xsl:when test="rating >= 9.0">4.5星</xsl:when>
<xsl:when test="rating >= 8.0">4星</xsl:when>
<xsl:when test="rating >= 7.0">3.5星</xsl:when>
<xsl:otherwise>普通</xsl:otherwise>
</xsl:choose>
<!-- 3. 库存状态标签 -->
<xsl:choose>
<xsl:when test="stock = 0">
<span class="soldout">售罄</span>
</xsl:when>
<xsl:when test="stock <= 10">
<span class="few">仅剩 <xsl:value-of select="stock"/> 件</span>
</xsl:when>
<xsl:otherwise>
<span class="ok">现货</span>
</xsl:otherwise>
</xsl:choose>
<!-- 4. 会员等级显示 -->
<xsl:choose>
<xsl:when test="@level = 'diamond'">钻石会员</xsl:when>
<xsl:when test="@level = 'gold'">黄金会员</xsl:when>
<xsl:when test="@level = 'silver'">白银会员</xsl:when>
<xsl:otherwise>普通会员</xsl:otherwise>
</xsl:choose>
<!-- 5. 图书分类彩色标签 -->
<xsl:choose>
<xsl:when test="category = '科幻'"><span class="tag sci-fi">科幻</span></xsl:when>
<xsl:when test="category = '文学'"><span class="tag lit">文学</span></xsl:when>
<xsl:when test="category = '历史'"><span class="tag history">历史</span></xsl:when>
<xsl:otherwise><span class="tag other"><xsl:value-of select="category"/></span></xsl:otherwise>
</xsl:choose>
<!-- 6. 折扣力度文字 -->
<xsl:choose>
<xsl:when test="discount <= 0.5">5折及以下</xsl:when>
<xsl:when test="discount <= 0.7">7折内</xsl:when>
<xsl:when test="discount <= 0.85">85折内</xsl:when>
<xsl:otherwise>无明显优惠</xsl:otherwise>
</xsl:choose>
<!-- 7. 出版时间段 -->
<xsl:choose>
<xsl:when test="year >= 2020">新书</xsl:when>
<xsl:when test="year >= 2010">近十年</xsl:when>
<xsl:when test="year >= 2000">21世纪</xsl:when>
<xsl:otherwise>经典老书</xsl:otherwise>
</xsl:choose>
<!-- 8. 复杂组合条件(多字段判断) -->
<xsl:choose>
<xsl:when test="price < 50 and rating >= 9">
<span class="super">神书特价!</span>
</xsl:when>
<xsl:when test="price < 30">
<span class="cheap">白菜价</span>
</xsl:when>
<xsl:otherwise>普通好书</xsl:otherwise>
</xsl:choose>
三、<xsl:choose> vs <xsl:if> 终极对比(2025 年真实建议)
| 项目 | <xsl:if>(多个嵌套) | <xsl:choose>(推荐) |
|---|---|---|
| 有 else | 必须再写一个反向 if | 天然支持 <xsl:otherwise> |
| 可读性 | 3个以上条件就看吐 | 像 switch,一目了然 |
| 维护性 | 改一个条件容易出错 | 修改超级安全 |
| 2025 年老手比例 | <10% | >90% |
| 强制推荐场景 | 只有单一条件 | 2个以上分支全部用 choose |
四、常见错误 vs 正确写法(99% 人都踩过)
| 想实现的效果 | 错误写法(会死) | 正确写法 |
|---|---|---|
| 有 else 逻辑 | 两个 <xsl:if> 反着写 | 必须用 <xsl:choose> + <xsl:otherwise> |
| 多个 when 同时满足 | 以为会都执行 | 只会执行第一个满足的 when(顺序很重要!) |
| 忘记写 otherwise | 什么都不输出就没了 | 强烈建议永远写 otherwise,防止空输出 |
| 条件写错符号 | <xsl:when test="price > 100"> | 必须写 >:test="price > 100" |
五、30 秒实战挑战(2025 年最常见需求)
把下面 XML 变成:
《三体》 ¥69 → ¥55.20 8折(限时特惠)
《活着》 ¥28 → ¥28 无优惠
《围城》 ¥38 → ¥19 5折(神价!)
<books>
<book><title>三体</title><price>69</price><discount>0.8</discount></book>
<book><title>活着</title><price>28</price></book>
<book><title>围城</title><price>38</price><discount>0.5</discount></book>
</books>
答案(一行 <xsl:choose> 完美搞定):
<xsl:for-each select="books/book">
《<xsl:value-of select="title"/>》 ¥<xsl:value-of select="price"/>
<xsl:choose>
<xsl:when test="discount">
→ ¥<xsl:value-of select="format-number(price * discount, '0.00')"/>
<xsl:choose>
<xsl:when test="discount <= 0.6"> <span style="color:red">(神价!)</span></xsl:when>
<xsl:when test="discount <= 0.8"> 8折(限时特惠)</xsl:when>
<xsl:otherwise> 有优惠</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
→ ¥<xsl:value-of select="price"/> 无优惠
</xsl:otherwise>
</xsl:choose>
<br/>
</xsl:for-each>
六、一句话总结(背下来就无敌)
任何时候只要出现“else”两个字,立即使用
<xsl:choose>,永不后悔!
需要我现在用 <xsl:choose> 帮你实现任何复杂逻辑?
- 评分 → 星级/文字评价
- 价格 → 颜色/标签/按钮状态
- 库存 → 不同提示文案
- 会员等级 → 不同图标+权益
- 订单状态 → 不同按钮(支付/取消/确认收货)
直接甩 XML + 需求,10 秒出完美成品!