我在尝试使用 SUDS 生成正确的 SOAP 请求时遇到问题。 对于某个 xml 元素,我需要使用命名空间指定属性:
ns0:type
原规范为:
(ParameterType){
Name =
(NameType){
value = None
_required = ""
}
Description = None
Value =
(ValueType){
Text = None
XmlDoc = None
_type = ""
}
}
所以我得到这个 xml:
<ns0:parameters>
<ns0:Input>
<ns0:Parameter>
<ns0:Name required="true">Param</ns0:Name>
<ns0:Value type="xs:Text">
<ns0:Text>1</ns0:Text>
</ns0:Value>
</ns0:Parameter>
</ns0:Input>
</ns0:parameters>
我需要什么才能得到它:
<ns0:parameters>
<ns0:Input>
<ns0:Parameter>
<ns0:Name required="true">Param</ns0:Name>
<ns0:Value ns0:type="xs:Text">
<ns0:Text>1</ns0:Text>
</ns0:Value>
</ns0:Parameter>
</ns0:Input>
</ns0:parameters>
我尝试使用插件,但我猜它不喜欢“:”字符。这是代码:
class MyPlugin(MessagePlugin):
def marshalled(self, context):
foo = context.envelope.getChild('Body').getChild('ns0:executeProcess').getChild('ns0:parameters').getChild('ns0:Input').getChild('ns0:Parameter').getChild('ns0:Value')
foo.attributes.append(Attribute("ns0:type", "Text"))
关于如何实现这一目标有什么想法吗?
更多信息:suds 0.4.1 - python 2.4
最佳答案
我找到了正确的插件用法:
class MyPlugin(MessagePlugin):
def marshalled(self, context):
inputElement = context.envelope.getChild('Body').getChild('ns0:executeProcess').getChild('ns0:parameters').getChild('ns0:Input')
parametrosElements = inputElement.getChildren()
for i in range( len( parametrosElements ) ):
valueElement = parametrosElements[i].getChild('ns0:Value')
valueElement.attributes.append(Attribute("ns0:type", "Text" ))
关于Python SUDS - 无法生成正确的 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8878645/