python xsd 在 soap 响应中
python xsd within soap response
我在服务器端使用 spyne 生成 Web 服务,python 与 excel 客户端(xmla 插件)通信,我在生成 soap 响应时遇到了一些困难与客户要求相匹配,
我想要的是像这样的 soap 响应(在 root 下标记一个 schema with "xsd:" 指的是 xmlns:xsd="http://www.w3.org/2001/XMLSchema 在 root:
<soap:Envelope xmlns="urn:schemas-microsoft-com:xml-analysis">
<soap:Body>
<DiscoverResponse xmlns="urn:...">
<return>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"
....>
< xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row" type="row"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="uuid">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="xmlDocument">
<xsd:sequence>
<xsd:any/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="row">
<xsd:sequence>
<xsd:element sql:field="PropertyName" name="PropertyName" type="xsd:string"/>
<xsd:element sql:field="PropertyDescription" name="PropertyDescription" type="xsd:string" minOccurs="0"/>
<xsd:element sql:field="PropertyType" name="PropertyType" type="xsd:string" minOccurs="0"/>
<xsd:element sql:field="PropertyAccessType" name="PropertyAccessType" type="xsd:string"/>
<xsd:element sql:field="IsRequired" name="IsRequired" type="xsd:boolean" minOccurs="0"/>
<xsd:element sql:field="Value" name="Value" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</root>
</return>
</DiscoverResponse>
</soap:Body>
我每次使用 spyne 得到的结果都不包含 xsd: :
<soap11env:Body>
<tns:DiscoverResponse>
<tns:return>
<ns2:root xmlns:ns2="urn:schemas-microsoft-com:xml-analysis:rowset">
<ns3:schema xmlns:ns3="services.models" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"/>
</ns2:root>
</tns:return>
</tns:DiscoverResponse>
这是我的 python 代码:
在 models.py 文件中:
class schema(ComplexModel):
targetNamespace = XmlAttribute(Unicode)
__type_name__ = "schema"
class DiscoverResponse(ComplexModel):
__namespace__ = "urn:schemas-microsoft-com:xml-analysis:rowset"
root = Array(schema.customize(max_occurs='unbounded'))
在 xmla.py 文件中:
class xmla_provider(ServiceBase):
@rpc(Unicode,
Restrictionlist,
Propertielist,
_returns=[DiscoverResponse],
_out_variable_names=["return"])
def Discover(ctx, RequestType, Restrictions, Properties):
response = DiscoverResponse()
response.root = schema(targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"),
return response
解决我尝试添加的问题
__ 命名空间 __ = "http://www.w3.org/2001/XMLSchema"
在 class 模式 中我遇到了这个问题
如果 child_ns != ns 而不是 child_ns 在 self.imports[ns] 和 \
键错误:'http://www.w3.org/2001/XMLSchema'
想到的另一个解决方案不是一个好的解决方案,是 强制 通过返回所有
肥皂响应
<xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">....
到 root 属性为 Unicode
这是代码:
在 xmla.py
response.root = "\n < xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='urn:schemas-microsoft-com:xml-analysis:rowset' ....
在model.py
class DiscoverResponse(ComplexModel):
__namespace__ = "urn:schemas-microsoft-com:xml-analysis:rowset"
root = Unicode
我得到了这个输出
DEBUG:spyne.protocol.xml:Response <soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:schemas-microsoft-com:xml-analysis">
<soap11env:Body>
<tns:DiscoverResponse>
<tns:return>
<ns0:root xmlns:ns0="urn:schemas-microsoft-com:xml-analysis:rowset">
< xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='urn:schemas-microsoft-com:xml-analysis:rowset'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:sql='urn:schemas-microsoft-com:xml-sql'
....
/xsd:schema
</ns0:root>
</tns:return>
</tns:DiscoverResponse>
所有< >字符都被替换为“<”和“>”
(正如我所说,这不是一个好的解决方案,通常我必须解决名称空间的问题)
请提出任何解决问题的建议,或者在最坏的情况下,python 中允许这种响应的 soap 库的任何建议
您需要切换到裸机模式,然后 return AnyXml
像这样:
class DiscoverRequest(ComplexModel):
RequestType = Unicode
Restrictions = Restrictionlist
Properties = Propertielist
class HelloWorldService(ServiceBase):
@rpc(DiscoverRequest, _returns=AnyXml, _body_style="bare")
def Discover(ctx, request):
return etree.fromstring("""<root>whatever</root>""")
我在服务器端使用 spyne 生成 Web 服务,python 与 excel 客户端(xmla 插件)通信,我在生成 soap 响应时遇到了一些困难与客户要求相匹配, 我想要的是像这样的 soap 响应(在 root 下标记一个 schema with "xsd:" 指的是 xmlns:xsd="http://www.w3.org/2001/XMLSchema 在 root:
<soap:Envelope xmlns="urn:schemas-microsoft-com:xml-analysis">
<soap:Body>
<DiscoverResponse xmlns="urn:...">
<return>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"
....>
< xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row" type="row"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="uuid">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="xmlDocument">
<xsd:sequence>
<xsd:any/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="row">
<xsd:sequence>
<xsd:element sql:field="PropertyName" name="PropertyName" type="xsd:string"/>
<xsd:element sql:field="PropertyDescription" name="PropertyDescription" type="xsd:string" minOccurs="0"/>
<xsd:element sql:field="PropertyType" name="PropertyType" type="xsd:string" minOccurs="0"/>
<xsd:element sql:field="PropertyAccessType" name="PropertyAccessType" type="xsd:string"/>
<xsd:element sql:field="IsRequired" name="IsRequired" type="xsd:boolean" minOccurs="0"/>
<xsd:element sql:field="Value" name="Value" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</root>
</return>
</DiscoverResponse>
</soap:Body>
我每次使用 spyne 得到的结果都不包含 xsd: :
<soap11env:Body>
<tns:DiscoverResponse>
<tns:return>
<ns2:root xmlns:ns2="urn:schemas-microsoft-com:xml-analysis:rowset">
<ns3:schema xmlns:ns3="services.models" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"/>
</ns2:root>
</tns:return>
</tns:DiscoverResponse>
这是我的 python 代码:
在 models.py 文件中:
class schema(ComplexModel):
targetNamespace = XmlAttribute(Unicode)
__type_name__ = "schema"
class DiscoverResponse(ComplexModel):
__namespace__ = "urn:schemas-microsoft-com:xml-analysis:rowset"
root = Array(schema.customize(max_occurs='unbounded'))
在 xmla.py 文件中:
class xmla_provider(ServiceBase):
@rpc(Unicode,
Restrictionlist,
Propertielist,
_returns=[DiscoverResponse],
_out_variable_names=["return"])
def Discover(ctx, RequestType, Restrictions, Properties):
response = DiscoverResponse()
response.root = schema(targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"),
return response
解决我尝试添加的问题
__ 命名空间 __ = "http://www.w3.org/2001/XMLSchema"
在 class 模式 中我遇到了这个问题
如果 child_ns != ns 而不是 child_ns 在 self.imports[ns] 和 \
键错误:'http://www.w3.org/2001/XMLSchema'
想到的另一个解决方案不是一个好的解决方案,是 强制 通过返回所有
肥皂响应<xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"
xmlns:sql="urn:schemas-microsoft-com:xml-sql">....
到 root 属性为 Unicode
这是代码:
在 xmla.py
response.root = "\n < xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='urn:schemas-microsoft-com:xml-analysis:rowset' ....
在model.py
class DiscoverResponse(ComplexModel):
__namespace__ = "urn:schemas-microsoft-com:xml-analysis:rowset"
root = Unicode
我得到了这个输出
DEBUG:spyne.protocol.xml:Response <soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:schemas-microsoft-com:xml-analysis">
<soap11env:Body>
<tns:DiscoverResponse>
<tns:return>
<ns0:root xmlns:ns0="urn:schemas-microsoft-com:xml-analysis:rowset">
< xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='urn:schemas-microsoft-com:xml-analysis:rowset'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:sql='urn:schemas-microsoft-com:xml-sql'
....
/xsd:schema
</ns0:root>
</tns:return>
</tns:DiscoverResponse>
所有< >字符都被替换为“<”和“>” (正如我所说,这不是一个好的解决方案,通常我必须解决名称空间的问题)
请提出任何解决问题的建议,或者在最坏的情况下,python 中允许这种响应的 soap 库的任何建议
您需要切换到裸机模式,然后 return AnyXml
像这样:
class DiscoverRequest(ComplexModel):
RequestType = Unicode
Restrictions = Restrictionlist
Properties = Propertielist
class HelloWorldService(ServiceBase):
@rpc(DiscoverRequest, _returns=AnyXml, _body_style="bare")
def Discover(ctx, request):
return etree.fromstring("""<root>whatever</root>""")