Search code examples
pythonxmlwsdlspyne

How to properly get spyne polymorphic tagnames


Is there a way to get the specific class tag names from my spyne class definitions?

Given the case, my class hierarchy looks like this:

# models.py
class Vehicle(ComplexModel):
    ...

class Car(Vehicle):
    ...

class Bike(Vehicle):
    ...

Then I followed the instructions in the answers of this questions, cause their problem seems similar, to get the polymorphism working.

how to implement abstract model in spyne

How do you @rpc _returns polymorphic types in spyne?

Important code lines which were changed:

# start.py
application=Application(
    ...
    in_protocol=Soap11(...),
    out_protocol=Soap11(polymorphic=True)
)

So far, so good. After i did this, i've got the following response:

<!-- Response.xml -->
...
<Vehicle xsi:type="Car">
    ...
</Vehicle>
...

So my question is:

Can i get rid of the general class name Vehicle as the tag name and get the name of the specific class e.g. Car as tag name instead?

Therefore the response should be something like this:

<Car>
    ...
</Car>

I've seen some approaches with the `ctx.out_string" and some text replacement in this question:

Remove the namespace from Spyne response variables

Which I think probably can work out fine for me, but if there is a "conventional" way in the API, which I did not find yet, I would prefer to get known of it to use it instead.


Solution

  • This is how XML polymorphism is supposed to work. libxml2's schema validator (that you use through lxml) should interpret it properly.

    OTOH, If you are sure you want distinction solely through tag names, have a look at the <choice> tag: http://w3.org/TR/xmlschema-1/#declare-contentModel

    Also see an article comparing the two approaches: http://ibm.com/developerworks/library/ws-tip-xsdchoice

    You seem to have already figured out how to use polymorphism in Spyne. So here's how you use the <choice> tag: https://github.com/arskom/spyne/blob/57ef5c0db51cb194353c67d317990fe89bc4177d/spyne/test/interface/test_xml_schema.py#L51