2014-12-03 33 views
1

我正在使用Django中的SUDS(4.0)創建使用Web Service的請求。 但是Suds沒有提供正確的命名空間。Python - Suds。如何編輯SOAP-ENV的命名空間:Envelope

SOAP信封我越來越:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope 
    xmlns:ns3="http://www.w3.org/2001/XMLSchema" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns1="http://factura360.com/invoice_webservice" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Header> 
     <ns2:LoginObject> 
      <soapusername>USER</soapusername> 
      <soappassword>PASSWORD</soappassword> 
     </ns2:LoginObject> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
     <ns1:generate_invoice> 
      <document xsi:type="xsd:string"> 
       <?xml version="1.0" encoding="UTF-8"?><cfdi:Comprobante></cfdi:Comprobante> 
      </document> 
      <documenttype xsi:type="xsd:int">0</documenttype> 
      <filetype xsi:type="xsd:int">1</filetype> 
      <cer xsi:nil="true"></cer><key xsi:nil="true"> 
      </key><password xsi:type="xsd:string">PASSWORD</password> 
     </ns1:generate_invoice> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

如何SOAP信封必須是:

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://factura360.com/invoice_webservice 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:ns2="namespace" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
    <SOAP-ENV:Header> 
      <ns2:LoginObject> 
       <soapusername>USER</soapusername> 
       <soappassword>PASSWORD</soappassword> 
      </ns2:LoginObject> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
     <ns1:generate_invoice> 
      <document xsi:type="xsd:string"> 
       <?xml version="1.0" encoding="UTF-8"?><cfdi:Comprobante> </cfdi:Comprobante> 
      </document> 
      <documenttype xsi:type="xsd:int">0</documenttype> 
      <filetype xsi:type="xsd:int">1</filetype> 
      <cer xsi:nil="true"></cer> 
      <key xsi:nil="true"></key> 
      <password xsi:type="xsd:string">PASSWORD</password> 
     </ns1:generate_invoice> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

因此,根據所需的SOAP信封,不應該有 '的xmlns:NS0' ,no'xmlns:ns3',應該有xmlns:xsd,值爲'http://www.w3.org/2001/XMLSchema','xmlns:ns2'應該有'namespace'的值。

我編輯的信封實際功能是這一個:

class CorrectNamespace(MessagePlugin): 
    def marshalled(self, context): 
     soap_env_parent = context.envelope 
     soap_env_parent.set('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema') 
     soap_env_parent.unset('xmlns:ns0') 
     soap_env_parent.unset('xmlns:ns3') 
     soap_env_parent.unset('xmlns:ns2') 
     soap_env_parent.set('xmlns:ns2', 'namespace') 

factura_client = Client(settings.FACTURA360_URL, plugins=[CorrectNamespace()]) 

#Setting the Login Object 
lg_element = Element('ns2:LoginObject') 
soapuser_element = Element('soapusername').setText(settings.FACTURA360_USER) 
soappass_element = Element('soappassword').setText(settings.FACTURA360_PASS) 
lg_element.append(soapuser_element) 
lg_element.append(soappass_element) 
factura_client.set_options(soapheaders=lg_element) 

#The call to the webservice 
invoice = factura_client.service.\ 
    generate_invoice(document, document_type, filetype, 
        cer, key, settings.FACTURA360_INVOICEPASSWORD) 

不幸的是我的插件功能,增加「的xmlns:XSD」和「的xmlns:NS2」,但不刪除他人。

這裏是插件功能所產生的信封:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope 
    xmlns:ns3="http://www.w3.org/2001/XMLSchema" 
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns0="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:ns1="http://factura360.com/invoice_webservice" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns2="namespace"> 

那我怎麼刪除別人的命名空間?

+0

嘗試suds-jurko包裝,看看問題是否仍然存在。 – 2014-12-03 23:55:08

+0

我認爲這裏沒有真正的問題,只是一個美學,對吧?命名空間名稱(例如「ns3」)只要使用一致就不重要。擁有額外的命名空間不是問題。 – 2014-12-04 08:15:30

+0

我試過suds-jurko的結果相同。 @FelixSchwarz這不只是額外的命名空間,某人被修改 – Bulos 2014-12-13 19:43:28

回答

0
class CorrectNamespace(MessagePlugin): 
    def marshalled(self, context): 
     del context.envelope.nsprefixes['ns3'] 
+2

儘管這段代碼可以回答這個問題,提供有關_why_ 額外的內容及/或_how_它回答 問題將顯著改善其長期 值。請[編輯]你的答案,添加一些解釋。 – 2016-04-15 16:23:29