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

XSL判断节点存在并实现姓名拼接、取值及首尾去空格的方法

XSLT实现方案

核心逻辑说明

利用XPath原生的normalize-space()函数即可满足需求,该函数会自动移除字符串首尾的空格,同时将字符串内部连续的多个空格合并为单个空格,完美适配你提到的空值、字段缺失的场景。

不同版本实现

XSLT 1.0(兼容性最好)

<xsl:variable name="fullName" select="normalize-space(concat(./Person/Name, ' ', ./Person/Surname))" />
<xsl:choose>
    <xsl:when test="$fullName != ''">
        <xsl:value-of select="$fullName" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="normalize-space(./Person/AlternativeName)" />
    </xsl:otherwise>
</xsl:choose>

XSLT 2.0及以上版本(写法更简洁)

可以直接用XPath的条件表达式一行实现:

<xsl:value-of select="
    let $fullName := normalize-space(concat(./Person/Name, ' ', ./Person/Surname))
    return if ($fullName != '') then $fullName else normalize-space(./Person/AlternativeName)
" />

边界场景验证

以上代码可覆盖你提到的所有前提条件:

  • Name或Surname为空/不存在时,拼接后产生的多余空格会被normalize-space()自动移除,不会出现首尾空格
  • 当Name和Surname全为空/全不存在时,$fullName计算结果为空字符串,自动触发AlternativeName的取值逻辑
  • AlternativeName的首尾空格同样会被处理,保证最终结果符合要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 12:06:02