XML:如何使用lxml生成空名称空间前缀和非空名称空间前缀?

时间:2020-01-06 06:26:59

标签: python xml lxml xml-namespaces canonicalization

我正在使用lxml库。

我需要使用所有Invoice来生成标签namespaces

第1条namespace通知没有prefix

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
xmlns:ccts="urn:un:unece:uncefact:documentation:2" 
xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1" 
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

我可以使用namespace生成没有前缀的第一个None,例如documentation

XHTML_NAMESPACE = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
XHTML = "{%s}" % XHTML_NAMESPACE

NSMAP = {None : XHTML_NAMESPACE} # the default namespace (no prefix)

invoice = etree.Element(XHTML + "Invoice", nsmap=NSMAP) 
print(etree.tostring(invoice, pretty_print=True, method="c14n2"))
#b'<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"></Invoice>'

但是由于可以将名称空间作为字典传递,因此我尝试将所有namespaces放入NSMAP字典中。

class XMLNamespaces:
   empty="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
   cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
   cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
   ccts="urn:un:unece:uncefact:documentation:2" 
   ds="http://www.w3.org/2000/09/xmldsig#" 
   ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
   qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
   sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1" 
   udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
   xsi="http://www.w3.org/2001/XMLSchema-instance"
# create XML 

NSMAP={None:XMLNamespaces.empty, 'cac':XMLNamespaces.cac, 'cbc':XMLNamespaces.cbc,
        'ccts':XMLNamespaces.ccts, 'ds':XMLNamespaces.ds, 'ext':XMLNamespaces.ext,
        'qdt':XMLNamespaces.qdt, 'sac':XMLNamespaces.sac, 'udt':XMLNamespaces.udt,
        'xsi':XMLNamespaces.xsi}

invoice = etree.Element("Invoice", nsmap=XMLNamespaces.empty) # lxml only!
#ublextensions = etree.SubElement(invoice, "UBLExtensions", nsmap=XMLNamespaces.ext)

print(etree.tostring(invoice, pretty_print=True, method="c14n2"))

内部发票标签,下一个是:

但是我得到了:

Traceback (most recent call last):
  File "xml-building.py", line 23, in <module>
    invoice = etree.Element("Invoice", nsmap=XMLNamespaces.empty) # lxml only!
  File "src\lxml\etree.pyx", line 3021, in lxml.etree.Element
  File "src\lxml\apihelpers.pxi", line 131, in lxml.etree._makeElement
  File "src\lxml\apihelpers.pxi", line 118, in lxml.etree._makeElement
  File "src\lxml\apihelpers.pxi", line 213, in lxml.etree._setNodeNamespaces
  File "src\lxml\apihelpers.pxi", line 274, in lxml.etree._iter_nsmap
  TypeError: 'in <string>' requires string as left operand, not NoneType

更新1:

我需要使用方法:c14n2。这是因为我需要按照签名部分中指示的CanonicalizationMethod格式化XML。

<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>

CanonicalizationMethod出现的签名部分:

<ds:Signature Id="signatureKG">
       <ds:SignedInfo>
         <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
         <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
          <ds:Reference URI="">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#envelopedsignature"/>
                </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
              <ds:DigestValue>ld6X+TvM42Fe+F1KM/OB jiKpnko=</ds:DigestValue>
                 </ds:Reference>
                  </ds:SignedInfo>
              <ds:SignatureValue>W6DbMHJEFmU7G uiU0O+HRUqVzQZZW3QndYtUyeL0VxXuTafHu2vBC+OXvnnali43VXRGQ+/E0tPl ZAssqI/PEPfzIU79Wufq6saxYGHKvzdnBi6hnaMuCSG5THHNFppx4aT1KNg7p/koBB3U8PT9C6m6 UnkJJNUquHkFc9BCqI8=</ds:SignatureValue>
             <ds:KeyInfo>
           <ds:X509Data>
         <ds:X509SubjectName>1.2 .840.113549.1.9.1=#161a4253554c434140534f55544845524e504552552e434f4d2e5045,CN=Carlos Vega,OU=10200545523,O=Vega Poblete Carlos Enrique,L=CHICLAYO,ST=LAMBAYEQUE,C=PE</ds:X509SubjectName>
      <ds:X509Certificate></ds:X509Certificate>
     </ds:X509Data>
   </ds:KeyInfo>
  </ds:Signature>

0 个答案:

没有答案