我正在尝试使用 suds version 0.4使用 python 版本2.7.1 访问soap api,但我在尝试创建 Client 时遇到错误目的。最简单的形式是这样的:
from suds.client import Client
url = 'http://tool-sb-api.hescloud.net/session/wsdl'
c = Client(url, cache=None)
产生的错误是:
suds.TypeNotFound: Type not found: '(retrieveSessionByIdResponse, http://hes.lbl.gov/scoring_tool/session, )'
我使用 ImportDoctor 尝试了一些变体根据documented方法,但我得到了同样的错误。
在检查 WSDL 和来自 suds 的调试消息后,它似乎有一个包含列表
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://hes.lbl.gov/scoring_tool/session" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="HesAPI_Session" targetNamespace="http://hes.lbl.gov/scoring_tool/session">
<types>
<xsd:schema targetNamespace="http://hes.lbl.gov/scoring_tool/session">
<xsd:include schemaLocation="http://tool-sb-api.hescloud.net/public/xsd/session/input/newSessionFromAddress.xsd"/>
<xsd:include schemaLocation="http://tool-sb-api.hescloud.net/public/xsd/session/output/newSessionFromAddressResponse.xsd"/>
<xsd:include schemaLocation="http://tool-sb-api.hescloud.net/public/xsd/session/input/newLabelSession.xsd"/>
...
相互引用。下面是一个执行此操作的 xsd 示例 ( newSessionFromAddressResponse.xsd )。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://hes.lbl.gov/scoring_tool/session" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://hes.lbl.gov/scoring_tool/session">
<xsd:element name="newSessionFromAddressResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="returnCode" type="xsd:int"/>
<xsd:element name="returnComment" type="xsd:string"/>
<xsd:element name="readOnly" type="xsd:string"/>
<xsd:element ref="tns:AddressList"/>
<!-- RetrieveSessionByIdResponse is defined in retrieveSessionByIdResponse.xsd. In the event of an error, we won't have a retrieveSessionByIdResponse, hence minOccurs=0 -->
<xsd:element ref="tns:retrieveSessionByIdResponse" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AddressList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zipcode" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
SOAP 水似乎有问题
<xsd:element ref="tns:retrieveSessionByIdResponse" minOccurs="0" maxOccurs="1"/>
在不同的文件中定义。
我不确定此 WSDL 是否遵循 WSI-BP 1.0正如此 API 的管理者所声称的那样,或者问题是由于 suds 无法正确处理“ref”属性。
无论哪种方式,我都会对成功创建的 suds Client 对象感到满意。
最佳答案
我通过创建 DocumentPlugin 解决了这个问题为了在解析之前将 xsd 文件的内容直接加载到文档中。
我必须覆盖 loaded函数并使用 minidom 执行此操作(尽管任何 xml 库都可以)。我无法覆盖 parsed
函数,因为我无法轻松创建泡沫 Element (它需要一个 options
变量,其中包含可能超出范围的信息)。
suds 记录 DocumentPlugin 的创建 here .
关于python - 解析包含相互引用的 XSD 的 WSDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075817/