0

我正在使用 spyne 在服务器端使用 python 生成 Web 服务以与 excel 客户端(xmla 插件)进行通信,并且我在生成与客户端请求匹配的肥皂响应时遇到了一些困难,我想要的是肥皂响应像这样(在标签下,带有“ xsd: ”的模式中引用xmlns:xsd="http://www.w3.org/2001/XMLSchema

<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

为了解决这个问题,我尝试在类模式中添加 __ namespace __ = " http://www.w3.org/2001/XMLSchema ", 如果 child_ns != ns 而不是 self.imports[ns] 中的 child_ns ,我会遇到这个问题和 \KeyError: ' http://www.w3.org/2001/XMLSchema '

另一个想到但不是一个好的解决方案的解决方案是通过返回所有来强制肥皂响应

<xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset" 
            xmlns:sql="urn:schemas-microsoft-com:xml-sql">....  

属性设置为 Unicode
这里是代码:在 xmla.py

  response.root = "\n  < xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='urn:schemas-microsoft-com:xml-analysis:rowset' ....

在模型.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">

      &lt; 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>

所有 < > 字符都替换为“& lt;” 和“>” (正如我所说,这不是一个好的解决方案,通常我必须解决名称空间的问题)

请提供解决问题的任何建议,或者在最坏的情况下,请提供任何关于 python 中允许这种响应的soap库的建议

4

1 回答 1

1

您需要切换到裸模式并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>""")
于 2016-07-20T12:28:01.493 回答