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

Wiremock提取XML属性用于Webhook时出现null无效XML错误求助

问题:XML属性提取在Webhook中返回null错误

处理API请求的XML报文时,需要提取属性并通过Webhook发送。已成功在API响应中提取该属性,但在serveEventListeners的Webhook配置中使用时,出现错误:attribute="[ERROR: null is not valid XML]"。


相关代码片段

XML请求

<ns2:ReqPay xmlns:ns2="http://npci.org/upi/schema/">
    <Head ver="2.0" ts="2021-01-17T11:18:11+05:30" orgId="929390493" msgId="randomMsgId"/>
    <Payer addr="shikhar@gios" name="Bobi Jacob" seqNum="1" type="PERSON" code="0000">
    </Payer>
</ns2:ReqPay>

Admin配置映射

{
    "request": {
        "method": "POST",
        "urlPattern": "/testing",
        "headers": {
            "Content-Type": {
                "equalTo": "application/xml"
            }
        }
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Type": "application/xml"
        },
        "transformers": [
            "response-template"
        ],
        "body": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:Ack xmlns:ns2=\"http://npci.org/upi/schema/\" api=\"npciApi\" reqMsgId=\"{{randomValue length=32 type='ALPHANUMERIC'}}\" addr=\"{{xPath request.body '/ReqPay/Payer/@addr'}}\" ts=\"{{now format='yyyy-MM-dd\'T\'HH:mm:ssXXX'}}\"/>"
    },
    "serveEventListeners": [
        {
            "name": "webhook",
            "parameters": {
                "method": "POST",
                "url": "https://nnccdd.requestcatcher.com/testing0100",
                "headers": {
                    "Content-Type": "application/xml"
                },
                "delay": {
                    "type": "fixed",
                    "milliseconds": 800
                },
                "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ns2:RespPay xmlns:ns2=\"http://npci.org/upi/schema/\" xmlns:ns3=\"http://npci.org/cm/schema/\">\n\t<Head msgId=\"xxx\" orgId=\"NPCI\" ts=\"{{now format='yyyy-MM-dd\'T\'HH:mm:ssXXX'}}\" ver=\"2.0\"/>\n\t<Ref addr=\"{{xPath request.body '/ReqPay/Payer/@addr'}}\" />\n\t</Resp>\n</ns2:RespPay>"
            }
        }
    ]
}

正常返回的API响应

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Ack xmlns:ns2="http://npci.org/upi/schema/" api="npciApi" reqMsgId="e4gqglqg1xadizkpnvkr3ls1j5666pjk" addr="arunl@gip1" ts="2024-05-28T05:39:52Z"/>

出错的Webhook响应

<?xml version="1.0" encoding="UTF-8"?>
<ns2:RespPay xmlns:ns2="http://npci.org/upi/schema/" xmlns:ns3="http://npci.org/cm/schema/">
    <Head msgId="xxx" orgId="NPCI" ts="2024-05-28T05:39:52Z" ver="2.0"/>
    <Ref addr="[ERROR: null is not valid XML]" />
    </Resp>
</ns2:RespPay>

问题原因分析

  1. XPath未处理XML命名空间:原始XML请求中的ReqPay和Payer元素属于http://npci.org/upi/schema/命名空间(前缀ns2),但Webhook中使用的XPath/ReqPay/Payer/@addr未指定命名空间,导致无法匹配目标元素,返回null。
  2. XML语法错误:Webhook的body中存在多余的</Resp>闭合标签,无对应起始标签,会导致XML格式异常。

修复方案

1. 修改XPath以支持命名空间

有两种方式处理命名空间:

  • 方式一:忽略命名空间匹配
    将Webhook body中的XPath替换为:
{{xPath request.body '/*[local-name()="ReqPay"]/*[local-name()="Payer"]/@addr'}}
  • 方式二:显式指定命名空间映射
    若模板引擎支持传递命名空间参数,可使用:
{{xPath request.body '/ns2:ReqPay/ns2:Payer/@addr' namespaces='{"ns2":"http://npci.org/upi/schema/"}'}}

2. 修复XML语法错误

删除Webhook body中多余的</Resp>标签。

3. 完整修正后的Admin Mapping

{
    "request": {
        "method": "POST",
        "urlPattern": "/testing",
        "headers": {
            "Content-Type": {
                "equalTo": "application/xml"
            }
        }
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Type": "application/xml"
        },
        "transformers": [
            "response-template"
        ],
        "body": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:Ack xmlns:ns2=\"http://npci.org/upi/schema/\" api=\"npciApi\" reqMsgId=\"{{randomValue length=32 type='ALPHANUMERIC'}}\" addr=\"{{xPath request.body '/ReqPay/Payer/@addr'}}\" ts=\"{{now format='yyyy-MM-dd\'T\'HH:mm:ssXXX'}}\"/>"
    },
    "serveEventListeners": [
        {
            "name": "webhook",
            "parameters": {
                "method": "POST",
                "url": "https://nnccdd.requestcatcher.com/testing0100",
                "headers": {
                    "Content-Type": "application/xml"
                },
                "delay": {
                    "type": "fixed",
                    "milliseconds": 800
                },
                "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ns2:RespPay xmlns:ns2=\"http://npci.org/upi/schema/\" xmlns:ns3=\"http://npci.org/cm/schema/\">\n\t<Head msgId=\"xxx\" orgId=\"NPCI\" ts=\"{{now format='yyyy-MM-dd\'T\'HH:mm:ssXXX'}}\" ver=\"2.0\"/>\n\t<Ref addr=\"{{xPath request.body '/*[local-name()=\"ReqPay\"]/*[local-name()=\"Payer\"]/@addr'}}\" />\n</ns2:RespPay>"
            }
        }
    ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 08:25:55