使用SUDS时添加xsi:type和envelope命名空间

som*_*off 1 python xml soap wsdl suds

我需要与SOAP服务进行交互,并且这样做很麻烦; 我真的很感激任何指针.原始错误消息是:

org.apache.axis2.databinding.ADBException: Any type element type has not been given
Run Code Online (Sandbox Code Playgroud)

经过一番研究,事实证明,这是SUDS与服务器之间的分歧,如何应对

type="xsd:anyType"
Run Code Online (Sandbox Code Playgroud)

在有问题的元素上.

我已经确认使用SOAPUI,经过建议,可以通过以下步骤解决问题:

  1. 将xsi:type ="xsd:string"添加到导致问题的每个元素
  2. 将xmlns:xsd ="http://www.w3.org/2001/XMLSchema"添加到SOAP信封中

那么,SUDS目前在哪里这样做:

<SOAP-ENV:Envelope ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Body>
  <ns0:method>
     <parameter>
        <values>
           <table>
              <key>EMAIL_ADDRESS</key>
              <value>example@example.org</value>
           </table>
        </values>
     </parameter>
  </ns0:method>
Run Code Online (Sandbox Code Playgroud)

它应该产生这个:

<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

  <ns3:Body>
  <ns0:method>
     ...
     <parameter>
        <values>
           <table>
              <key xsi:type="xsd:string">EMAIL_ADDRESS</key>
              <value xsi:type="xsd:string">example@example.org</value>
           </table>
        </values>
     </parameter>
  </ns0:method>
Run Code Online (Sandbox Code Playgroud)

有没有正确的方法来做到这一点?我已经看到了使用ImportDoctor或MessagePlugins的建议,但还没有真正了解如何实现所需的效果.

som*_*off 10

我发现的解决方案是使用MessagePlugin在发送之前基本上手动修复XML.我希望有更优雅的东西,但至少这有用:

class SoapFixer(MessagePlugin):

    def marshalled(self, context):
        # Alter the envelope so that the xsd namespace is allowed
        context.envelope.nsprefixes['xsd'] = 'http://www.w3.org/2001/XMLSchema'
        # Go through every node in the document and apply the fix function to patch up incompatible XML. 
        context.envelope.walk(self.fix_any_type_string)

    def fix_any_type_string(self, element):
        """Used as a filter function with walk in order to fix errors.
        If the element has a certain name, give it a xsi:type=xsd:string. Note that the nsprefix xsd must also
         be added in to make this work."""
        # Fix elements which have these names
        fix_names = ['elementnametofix', 'anotherelementname']
        if element.name in fix_names:
            element.attributes.append(Attribute('xsi:type', 'xsd:string'))
Run Code Online (Sandbox Code Playgroud)