2016-07-19 106 views
0

我使用spyne在服務器端使用python與excel客戶端(xmla插件)​​進行通信來生成web服務,並且我生成肥皂響應與客戶端請求匹配時遇到了一些困難, 我要的是這樣一個SOAP響應(下標籤模式與 「XSD:」 是指的xmlns:XSD =「http://www.w3.org/2001/XMLSchema肥皂響應中的python xsd

<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')) 

in xmla.py file:

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」 在類架構 和我得到這個問題
如果child_ns!=在self.imports [NS] NS,而不是child_ns和\ KeyError異常:「http://www.w3.org/2001/XMLSchema

想到的和這不是一個好的解決方案另一種解決方案,是通過返回所有

SOAP響應

attribut爲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;」和「& gt」; (正如我所說這不是一個好的解決方案,通常我必須解決名稱空間的問題)

請任何建議來解決問題或在最壞的情況下,在Python中的任何建議,樣的反應

+0

我感到困惑,每個人都爲晚問Spyne問題都沒有在它Spyne代碼的單個斑點。我需要看代碼!錯誤!來吧,幫我幫你! –

+0

你設置了_returns = AnyXml嗎?你如何產生迴應? –

+0

抱歉不清楚,我更新了我的問題@BurakArslan –

回答

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>""") 
+0

就是這樣! 非常感謝! :d –