WSO2 Integration Studio中无法将XML响应值捕获到Property Mediator
问题:WSO2 Integration Studio中XPath提取XML命名空间节点失败
问题场景
使用WSO2 Integration Studio创建的API接收如下XML响应:
<ns3:result xmlns:response="http://inforwisse.co.ts/broker/response" xmlns:ns3="http://inforwise.co.tz/broker/" xmlns:request="http://inforwisse.co.ts/broker/request"> <ns3:resultType>Completed</ns3:resultType> <ns3:resultCode>0</ns3:resultCode> <ns3:resultDesc>Success</ns3:resultDesc> <ns3:originatorConversationID>025d7efd-58bc-b06b-2a9b91cdd3d1</ns3:originatorConversationID> <ns3:conversationID>025d7efd-58bc-b06b-2a9b91cdd3d1</ns3:conversationID> <ns3:mpesaReceipt>5BL716QNJLM</ns3:mpesaReceipt> <ns3:transactionID>125189978</ns3:transactionID> <ns3:resultParameters> <ns3:parameter> <ns3:key>Amount</ns3:key> <ns3:value>Tsh15,000</ns3:value> </ns3:parameter> <ns3:parameter> <ns3:key>Transaction Datetime</ns3:key> <ns3:value>15/03/2018 19:43:15</ns3:value> </ns3:parameter> <ns3:parameter> <ns3:key>Working Account Available Funds</ns3:key> <ns3:value>0.00</ns3:value> </ns3:parameter> <ns3:parameter> <ns3:key>Utility Account Available Funds </ns3:key> <ns3:value>0.00</ns3:value> </ns3:parameter> <ns3:parameter> <ns3:key>Charges Paid Account Available Funds </ns3:key> <ns3:value>0.00</ns3:value> </ns3:parameter> </ns3:resultParameters> </ns3:result>
尝试用以下Property Mediator配置提取resultType的值:
<property expression="//ns3:resultType/text()" name="resultType" scope="default" type="STRING"/>
但调用Log Mediator打印时触发错误,日志信息如下:
[2023-09-28 17:00:12,174] INFO {LogMediator} - {api:mpesa-agent-v1-api:v1.0.0} To: /mpesa-agent/1.0.0/cashin/callback, MessageID: urn:uuid:82c9e0d0-a1ad-425f-b870-414d43cd767d, correlation_id: 82c9e0d0-a1ad-425f-b870-414d43cd767d, Direction: request, MESSAGE = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = Evaluation of the XPath expression //ns3:resultType/text() resulted in an error, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns3:result xmlns:ns3="http://inforwise.co.tz/broker/" xmlns:request="http://inforwise.co.tz/broker/request" xmlns:response="http://inforwise.co.tz/broker/response">
无法成功保存resultType的值。
解决方案
核心问题是XPath表达式未在配置中绑定对应的命名空间。WSO2处理带命名空间的XML时,必须显式声明XPath中使用的前缀与命名空间URI的映射关系。
1. 声明命名空间
在API的<api>标签或使用的Sequence的<sequence>标签内,添加ns3命名空间的声明:
<!-- 示例:在API标签内添加 --> <api xmlns:ns3="http://inforwise.co.tz/broker/" name="mpesa-agent-v1-api" context="/mpesa-agent/1.0.0" version="v1.0.0"> <!-- API配置内容 --> </api>
如果XPath中用到了soapenv前缀(比如精确路径匹配),也要同时声明该命名空间:
<api xmlns:ns3="http://inforwise.co.tz/broker/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ...>
2. 修正XPath表达式(可选)
原表达式可以保留,但为了避免匹配到其他无关的同名节点,建议使用更精确的路径:
<property expression="//soapenv:Body/ns3:result/ns3:resultType/text()" name="resultType" scope="default" type="STRING"/>
3. 验证提取结果
添加Log Mediator打印提取到的属性值,确认是否成功:
<log level="custom"> <property name="Extracted resultType" expression="$ctx:resultType"/> </log>
内容的提问来源于stack exchange,提问作者Kavishka Rajapakshe
相关产品推荐
相关产品推荐

