Search code examples
pythonxsdzeepspml

How can I use an additional xsd with python zeep?


I need to implement a SPML interface, which in the end is performing a SOAP request over HTTP(s). I have a wsdl for it which boils down to this:

<wsdl:types>
  <schema targetNamespace="http://soapadapter.something" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="receiveRequest" type="xsd:anyType"/>
  </schema>
</wsdl:types>
[...]
<wsdl:operation name="receiveRequest">
 <wsdl:input message="impl:receiveRequestRequest" name="receiveRequestRequest"/>
</wsdl:operation>

As you can see, the only defined request element is of type "xsd:anyType". I have a separate xsd, not at all linked in the wsdl, which describes how the request should be formed.

I'd like to use zeep to implement a SOAP request for consuming the interface. How can I make zeep aware of that (local) xsd file?

I have found the zeep.xsd.schema.SchemaDocument class, but no example of it being used anywhere. Can someone give me a usage example of how to create a client that uses an wsdl and separate xsd file?


Solution

  • Yes, you can add additional schemas to your zeep client the following way:

    import os
    from zeep.loader import load_external
    from zeep import Client
    
    
    XSD_SCHEMA_FILE = "/path/to/your.xsd"
    CONTAINER_DIR = os.path.dirname(XSD_SCHEMA_FILE)  # Where to load dependencies if any
    
    client = Client('https://path/to/your.wsdl')
    schema_doc = load_external(open(XSD_SCHEMA_FILE, "rb"), None)
    doc = client.wsdl.types.create_new_document(schema_doc, f"file://{CONTAINER_DIR}")
    doc.resolve()