title: XML: generate tags with prefix from namespaces
body: <p>I need to generate an XML using different prefix that represent namespaces:</p>
  
  <p>For example, I've this list of namespaces:</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>And I'd like to generate this structure:</p>
  
  <p><strong>Only the Invoice tag has the full namespaces.</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" standalone="no"?&gt;
  &lt;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"&gt;
      &lt;ext:UBLExtensions&gt;
       &lt;ext:UBLExtension&gt;
           &lt;ext:ExtensionContent&gt;
              &lt;sac:AdditionalInformation&gt;
              &lt;/sac:AdditionalInformation&gt;
           &lt;/ext:ExtensionContent&gt;
       &lt;/ext:UBLExtension&gt;
  &lt;/Invoice&gt;
  </code></pre>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>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))
  </code></pre>
  
  <p>But it returns:</p>
  
  <pre><code>File "xml-building.py", line 27, in &lt;module&gt;
      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 &lt;string&gt;' requires string as left operand, not NoneType
  </code></pre>