在soap响应中的python xsd

时间:2016-07-19 13:42:20

标签: python soap spyne xmla

我正在使用spyne在服务器端使用python生成Web服务以与excel客户端(xmla插件)​​进行通信,并且我在生成与客户端请求匹配的soap响应时遇到一些困难, 我想要的是像这样的肥皂响应(在 root 标记架构 xsd:“,引用 xmlns:xsd = root 中的“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” 在类架构,我遇到了这个问题     if 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">....  

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">

      &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; &GT;字符被“&amp; lt;”取代和“&amp; gt;” (正如我所说这不是一个好的解决方案,通常我必须解决名称空间的问题)

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

1 个答案:

答案 0 :(得分: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>""")