0

I'm using Python LXML etree to parse XML addenda and I need to get rid of ns0 NAMESPACE

I already tried the solution described in the link below but did not get the result I'm looking for.

Remove namespace and prefix from xml in python using lxml

My code is like this:

from lxml import etree, objectify

tree = etree.parse(TEMPdir+FILEname)
addendaData = tree.getroot()

cfdi = "cfdi" #"http://www.sat.gob.mx/cfd/3"
dsf  = "http://tempuri.org/DSFactura.xsd"
ns3  = {"dsf": dsf}

addenda = etree.SubElement(addendaData, "{" + cfdi + "}" + "Addenda",)

cargaRemision = etree.SubElement(addenda, "DSCargaRemisionProv",
    nsmap = ns3
)

With this code I'm getting XML:

...
  <ns0:Addenda xmlns:ns0="http://www.sat.gob.mx/cfd/3">
    <DSCargaRemisionProv xmlns:dsf="http://tempuri.org/DSFactura.xsd">
      <Remision ...>
    </DSCargaRemisionProv>
  </ns0:Addenda>

And what I need to get is this:

...
 <cfdi:Addenda>
    <DSCargaRemisionProv xmlns:dsf="http://tempuri.org/DSFactura.xsd">
      <Remision ...>
    </DSCargaRemisionProv>
  </cfdi:Addenda>
Hugo
  • 3
  • 2
  • I think we need to see minimal but complete samples with all necessary namespace declarations present; showing an element `cdfi:Addenda` with a namespace prefix but no namespace declaration using that prefix doesn't allow us to understand which input you have and which changes you want to make. Also, do you need to solve that with element tree or could you use XSLT? – Martin Honnen May 06 '23 at 09:46

0 Answers0