【问题标题】:Python - Suds. How to edit the namespaces of SOAP-ENV:EnvelopePython - 泡沫。如何编辑 SOAP-ENV:Envelope 的命名空间
【发布时间】:2015-02-01 17:28:47
【问题描述】:

我正在创建一个使用 Django 中的 SUDS (4.0) 来使用 Web 服务的请求。 但是 Suds 没有放置正确的命名空间。

我得到的肥皂信封:

<?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”,没有“xmlns:ns3”,应该有值为“http://www.w3.org/2001/”的 xmlns:xsd 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">

如何删除其他命名空间?

【问题讨论】:

  • 试试 suds-jurko 包,看看问题是否仍然存在。
  • 我认为这里没有真正的问题,只是审美问题,对吧?命名空间名称(例如“ns3”)并不重要,只要它们被一致使用。拥有额外的命名空间不是问题。
  • 我尝试过 suds-jurko,结果相同。 @FelixSchwarz 这不仅仅是额外的命名空间,有人被修改了

标签: python namespaces suds


【解决方案1】:
class CorrectNamespace(MessagePlugin):
    def marshalled(self, context):
        del context.envelope.nsprefixes['ns3']

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关 why 和/或 如何 回答问题的额外上下文将显着改善其长期价值。请edit你的答案添加一些解释。
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
相关资源
最近更新 更多