我需要使用代表名称空间的不同前缀来生成XML:
例如,我有以下名称空间列表:
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标签具有完整的名称空间。
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<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">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<sac:AdditionalInformation>
</sac:AdditionalInformation>
</ext:ExtensionContent>
</ext:UBLExtension>
</Invoice>
我的代码:
from lxml import etree
from lxml.etree import Element, SubElement, QName, tostring
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"
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=NSMAP) # lxml only!
ublextensions = etree.SubElement(invoice, "UBLExtensions", nsmap=NSMAP['ext'])
print(etree.tostring(invoice, pretty_print=True))
但它返回:
File "xml-building.py", line 27, in <module>
ublextensions = etree.SubElement(invoice, "UBLExtensions", nsmap=NSMAP['ext'])
File "src\lxml\etree.pyx", line 3135, in lxml.etree.SubElement
File "src\lxml\apihelpers.pxi", line 199, in lxml.etree._makeSubElement
File "src\lxml\apihelpers.pxi", line 193, in lxml.etree._makeSubElement
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
您无需nsmap
在子元素中添加。删除该名称并在每个元素的ID之前的大括号中添加所需的名称空间(请参见下文,为此我使用了另外的字典)。它将根据您的需要工作:
from lxml import etree
from lxml.etree import Element, SubElement, QName, tostring
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"
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}
ns = {k:'{' + v + '}' for k,v in NSMAP.items()}
invoice = etree.Element("Invoice", nsmap=NSMAP) # lxml only!
ublextensions = etree.SubElement(invoice, f"{ns['ext']}UBLExtensions")
ublextension = etree.SubElement(ublextensions, f"{ns['ext']}UBLExtension")
extcontent = etree.SubElement(ublextension, f"{ns['ext']}ExtensionContent")
addinfo = etree.SubElement(extcontent, f"{ns['sac']}AdditionalInformation")
print(etree.tostring(invoice, pretty_print=True).decode('ascii'))
结果:
<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">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<sac:AdditionalInformation/>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
</Invoice>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句