You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XML中显示value-of取值前检查目标值是否含特定字符?

修正你的XSLT条件判断代码

你的代码存在几处问题,调整后即可实现需求:

  1. contains函数参数错误:你颠倒了检查目标与查找字符串的顺序,且应传入节点内容而非字符串'actualValue'
  2. 标签未闭合:<xsl:value-of>标签缺少闭合符号/>
  3. 结构问题:需要将值放在<div>内部,才能让内容完整包裹在div容器中

修正后的代码如下:

<xsl:if test="contains(/root/parent/child/value[@name='actualValue'], 'abc')">
  <div>Actual Value is <xsl:value-of select="/root/parent/child/value[@name='actualValue']" /></div>
</xsl:if>

补充优化方案:

  • 若节点可能为空,可添加非空判断避免报错:
<xsl:if test="/root/parent/child/value[@name='actualValue'] and contains(/root/parent/child/value[@name='actualValue'], 'abc')">
  <div>Actual Value is <xsl:value-of select="/root/parent/child/value[@name='actualValue']" /></div>
</xsl:if>
  • 使用<xsl:variable>复用节点选择,让代码更简洁易维护:
<xsl:variable name="actualValue" select="/root/parent/child/value[@name='actualValue']" />
<xsl:if test="$actualValue and contains($actualValue, 'abc')">
  <div>Actual Value is <xsl:value-of select="$actualValue" /></div>
</xsl:if>

内容的提问来源于stack exchange,提问作者JZnrgn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:52:13