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

求助:XSLT中substring-before函数为何返回空白值?

解决XSLT substring-before返回空白的问题

错误原因

你犯了核心错误:把节点名称/路径用单引号包裹成字符串传递给substring-before函数。

  • substring-before('wd:Email','@')处理的是字符串"wd:Email",这个字符串里没有@,所以返回空白
  • substring-before('wd:Email/text()','@')处理的是字符串"wd:Email/text()",同样不含@,结果也是空白

substring-before的第一个参数需要的是节点的文本值或真实字符串内容,而非字符串形式的节点路径。

正确写法

首先确保XSLT声明了XML中wd前缀对应的命名空间(替换为你实际使用的命名空间URI),然后直接选取节点即可:

完整XSLT示例

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wd="http://your-namespace-uri">
  <xsl:template match="/">
    <!-- 写法1:直接选取wd:Email节点,函数自动提取其文本值 -->
    <xsl:value-of select="substring-before(wd:Report_Entry/wd:Email, '@')"/>
    
    <!-- 写法2:显式选取文本节点,效果与写法1一致 -->
    <!-- <xsl:value-of select="substring-before(wd:Report_Entry/wd:Email/text(), '@')"/> -->
  </xsl:template>
</xsl:stylesheet>

运行后会正确输出robert.henry(注:XML中Email字段值为小写的robert.henry,若需得到大写开头的Robert.henry,需确保XML中的对应值正确)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 00:27:20