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

如何在Karate DSL中从SOAP响应获取指定标签属性值?

问题分析与解决方法

核心错误点

  1. 节点名称不匹配:你的SOAP响应里的目标节点是PatientNotificationResponse,但代码中错误写为PatientNotificationRequest,这是导致返回#notpresent的主要原因。
  2. XPath属性定位语法错误:获取XML元素的属性值时,必须使用@属性名的语法,而非直接写属性名。

正确的验证与属性获取代码

验证status属性是否为success

* def responseStatus = /SOAP-ENV:Envelope/SOAP-ENV:Body/PatientNotificationResponse/@status
* def isSuccess = responseStatus == "success"
* print isSuccess

获取其他属性的示例

  • 获取messageDate:
* def msgDate = /SOAP-ENV:Envelope/SOAP-ENV:Body/PatientNotificationResponse/@messageDate
* print msgDate
  • 获取messageID:
* def msgId = /SOAP-ENV:Envelope/SOAP-ENV:Body/PatientNotificationResponse/@messageID
* print msgId

简化写法(部分框架支持)

如果你的测试框架(如Karate)支持忽略命名空间的简化写法,也可以这样写:

* def isSuccess = /Envelope/Body/PatientNotificationResponse/@status == "success"
* print isSuccess

不过建议保留命名空间写法,避免因命名空间配置问题导致定位失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 14:07:05