从 node-soap 服务器响应中删除命名空间前缀

问题描述 投票:0回答:1

这是来自我的 SOAP 服务器的响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost:8080/soap/server">
<soap:Body>
<tns:pingResponse>
<tns:name>e3499cd86f59508466e8c6fcd37aabc8</tns:name>
</tns:pingResponse>
</soap:Body>
</soap:Envelope>

我需要的是身体内部的响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost:8080/soap/server">
<soap:Body>
<pingResponse> // <<<< no TNS
<name>e3499cd86f59508466e8c6fcd37aabc8</name> // <<<< no TNS
<pingResponse> // <<<< no TNS
</soap:Body>
</soap:Envelope>

我从 stackoverflow 和其他我发现的东西中尝试了很多,但无法解决这个问题:/

这是我的wsdl:

<wsdl:definitions
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://localhost:8080/soap/server"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    name="Cos_Service_Soap"
    targetNamespace="http://localhost:8080/soap/server"
>
    <wsdl:binding name="Cos_Service_SoapBinding" type="tns:Cos_Service_SoapPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="ping">
            <soap:operation soapAction="http://localhost:8080/soap/server#ping" />
            <wsdl:input>
                <soap:body use="literal" namespace="http://framework.zend.com" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" namespace="http://framework.zend.com" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
<service name="Cos_Service_SoapService">
        <port name="Cos_Service_SoapPort" binding="Cos_Service_SoapBinding">
            <soap:address location="http://localhost:8080/soap/server" />
        </port>
    </service>
<message name="pingIn" />
    <message name="pingResponse">
        <part name="return" type="xsd:string" />
    </message>
    <message name="pingOut">
        <part name="return" type="xsd:boolean" />
        <part name="name" type="xsd:boolean" />
    </message>
</wsdl:definitions>

启动服务器/侦听的功能:

function startServer() {
    server.listen(port);
    server.on('error', onError); 
    server.on('listening', onListening);

    soap.listen(server, {
        path: '/soap/server',
        services: poService.service,
        xml: poService.xml,
    });
}

和服务:

var poService = {
  Cos_Service_SoapService: {
    Cos_Service_SoapPort: {   
          ping: function(args) {  
              console.log(args);            
              return {'name': args.token};       
          },           
      } 
   }
}; 

如果我删除:

xmlns:tns="http://localhost:8080/soap/server
从 wsdl 然后
tns:
更改为
undefined:
.

如果我从

style="rpc"
中删除
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
,它会显示错误(来自这个答案https://stackoverflow.com/a/70478488/8522950):

<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:tns="http://localhost:8080/soap/server">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>SOAP-ENV:Server</soap:Value>
<soap:Subcode>…</soap:Subcode>
</soap:Code>
<soap:Reason>
<soap:Text>TypeError: Cannot read properties of undefined (reading &apos;outputName&apos;)</soap:Text>
</soap:Reason>
</soap:Fault>
</soap:Body>
</soap:Envelope>

我在 npm node-soap 上找不到任何关于删除此前缀或从响应中替换它的信息。

node.js soap wsdl
1个回答
0
投票

我在

serverSoap.on('response')..
用地图做了经典替换

soapServer.on('response', (res) => {
    const replaceMap = {
        'ns1:return': 'return',
        'tns:return': 'return',
    };
    res.result = res.result.replace(
        new RegExp(Object.keys(replaceMap).join('|'), 'g'),
        (m) => replaceMap[m]
    );
});
© www.soinside.com 2019 - 2024. All rights reserved.