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

Python lxml XPath无法提取SOAP响应中WebServiceFault的message元素

解决SOAP响应中下元素提取失败的问题

问题核心是命名空间处理遗漏:

  • <WebServiceFault>及其子元素<message>属于专属命名空间http://www.taleo.com/ws/integration/toolkit/2005/07,但你定义的命名空间字典未包含该空间,导致XPath无法匹配到目标节点。
  • 原命名空间字典里的ns1值末尾多了一个多余的双引号,虽未影响之前的匹配,但属于无效定义。

修正方案:

  1. 更新命名空间字典,添加taleo前缀对应目标命名空间,同时修正ns1的错误:
ns = { 
    'soap':'http://www.w3.org/2003/05/soap-envelope', 
    'ns1':'http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd',
    'taleo':'http://www.taleo.com/ws/integration/toolkit/2005/07'
}
  1. 使用taleo前缀调整XPath语句,定位目标元素:
# 提取文本,可按需用strip()去除首尾空白
print(root.xpath('//soap:Detail/taleo:WebServiceFault/taleo:message', namespaces=ns)[0].text.strip())

执行结果:

AccessResult: result: Access Denied | AuthenticationAsked: true | ErrorCode:
            IDP_ERROR: 137 |
            ErrorReason: null

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 02:21:11