pythonpython-2.7spyne

how to omit tns from response and change tag name in spyne?


how do omit tns from my response and also change the tag name.? my response is like this

<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="spyne.example">
  <soap11env:Body>
    <tns:FnSchedule_CityResponse>
      <tns:FnSchedule_CityResult>
        <tns:ErrorString></tns:ErrorString>
        <tns:CityName>HYDERABAD</tns:CityName>
        <tns:CityId>1</tns:CityId>
        <tns:ErrId>0</tns:ErrId>
      </tns:FnSchedule_CityResult>
    </tns:FnSchedule_CityResponse>
  </soap11env:Body>
</soap11env:Envelope>

I want to remove tns and change "soap11env" to "soap". Having these values is causing validation issues.

i referred this question on stack overflow, implemented it, but was not helpful. Remove the namespace from Spyne response variables


Solution

  • In order to change soap11env to soap just simply override the response using

    application.interface.nsmap['soap'] = application.interface.nsmap['soap11env']
    

    The 'tns' or target namespaces must not be change but there may arrive a few cases, One might need to change a name in order to completely test something.

    To change the namespaces,

    def on_method_return_string(ctx):
    ctx.out_string[0] = ctx.out_string[0].replace(b'ns4', b'diffgr')
    ctx.out_string[0] = ctx.out_string[0].replace(b'ns5', b'msdata')
    

    YourModelClassName.event_manager.add_listener('method_return_string', on_method_return_string)

    What I did here was, replace the namespace ns4 with diffgr and ns5 with msdata. ns4 and ns5 are sub name spaces that I had in the responses of some third party application. This solution I found in the mailing list maintained for spyne.