我正在python 2.7和django 1.9中使用spyne 2.12.14我想返回这样的响应:
?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns2:GetResponse xmlns:ns2="http://test.example.com/test">
<return>
<myHeader>
<id>1234abc</id>
<code>000</code>
<message>Success</message>
</myHeader>
<MyDetail>
<item1>myItem1</item1>
<item2>myItem2</item2>
</MyDetail>
<MyDetail>
<item1>myItem1</item1>
<item2>myItem2</item2>
</MyDetail>
</return>
</ns2:GetResponse>
</soapenv:Body>
</soapenv:Envelope>
但得到的回应是:
?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns2:GetResponse xmlns:ns2="http://test.example.com/test">
<return>
<myHeader>
<id>1234abc</id>
<code>000</code>
<message>Success</message>
</myHeader>
<MyDetail>
<MyDetail>
<item1>myItem1</item1>
<item2>myItem2</item2>
</MyDetail>
<MyDetail>
<item1>myItem1</item1>
<item2>myItem2</item2>
</MyDetail>
</MyDetail>
</return>
</ns2:GetResponse>
</soapenv:Body>
</soapenv:Envelope>
这是我的代码:
class MyHeader(ComplexModel):
__namespace__ = 'http://test.example.com/test'
INHERITANCE = None,
INDICATOR = Sequence,
_type_info = {
'id': String,
'code': String,
'message': String,
}
class MyDetail(ComplexModel):
__namespace__ = 'http://test.example.com/test'
INHERITANCE = None,
INDICATOR = Sequence,
_type_info = {
'item1': String,
'item2': String,
}
class GetResponse(ComplexModel):
INHERITANCE = None,
INDICATOR = Sequence,
_type_info = {
'myHeader': MyHeader,
'MyDetail': Array(MyDetail, minOccurs=0, maxOccurs='unbounded')
}
@rpc(MyObject, _returns=[GetResponse], _out_variable_names=["return"])
def GetMiniStatement(ctx, MyObjectInfo):
do_something
有人能帮忙吗?
您想要一个非包装数组。
删除此:
'MyDetail': Array(MyDetail, minOccurs=0, maxOccurs='unbounded')
添加此:
'MyDetail': MyDetail.customize(minOccurs=0, maxOccurs='unbounded')
顺便说一句,您的数组定义似乎很奇怪,因为通过将Array类型的maxOccurs设置为大于1的值,您正在创建一个非包装数组的包装数组,而该数组似乎与您想要的不一样。(换句话说,你是在告诉Spyne,数组类型可以发生不止一次,而不是里面的内容。(
将我的声明更改为"MyDetail":数组(MyDetail,maxOccurs="unbounded",wrapped=False(它是有效的。