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

XSLT如何测试节点值?请求状态判断语法问题咨询

问题分析与解决方案

原XSLT代码无法正常工作,主要存在两个核心问题:

  1. 缺少必要的XSLT结构标签:xsl:when和xsl:otherwise必须包裹在xsl:choose内部才符合语法规范
  2. XPath表达式错误://.[local-name()='RequestStatus']是无效写法,同时未正确处理XML的命名空间

正确写法一:忽略命名空间匹配节点

<form:field>
    <form:name>request_status</form:name>
    <xsl:choose>
        <xsl:when test="//*[local-name()='RequestStatus']/text() = 'A'">
            <form:value>Accepted</form:value>
        </xsl:when>
        <xsl:otherwise>
            <form:value>Refused</form:value>
        </xsl:otherwise>
    </xsl:choose>
</form:field>

正确写法二:声明并使用命名空间(更规范)

如果在XSLT头部声明对应的命名空间前缀,代码可读性和准确性会更高:

<!-- 先在XSLT根节点声明命名空间 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:epay="http://altsq/XMLSchema/epayments/standard"
    xmlns:form="你的form命名空间URI">

<!-- 然后使用命名空间前缀匹配节点 -->
<form:field>
    <form:name>request_status</form:name>
    <xsl:choose>
        <xsl:when test="//epay:RequestStatus/text() = 'A'">
            <form:value>Accepted</form:value>
        </xsl:when>
        <xsl:otherwise>
            <form:value>Refused</form:value>
        </xsl:otherwise>
    </xsl:choose>
</form:field>
</xsl:stylesheet>

关键说明

  • 必须用**<xsl:choose>**包裹xsl:when和xsl:otherwise,这是XSLT的强制语法要求,原代码缺少该标签会直接导致语法错误
  • XPath中//*[local-name()='RequestStatus']表示匹配任意层级下名为RequestStatus的节点(忽略命名空间),而原代码的//.[local-name()='RequestStatus']是错误写法,.代表当前节点,不能与节点名称条件这样组合使用
  • 优先推荐使用命名空间前缀的写法,可避免不同命名空间下同名节点的混淆,逻辑更清晰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 21:21:11