Skip to content
Advertisement

Remove namespace and prefix from xml in python using lxml

I have an xml file I need to open and make some changes to, one of those changes is to remove the namespace and prefix and then save to another file. Here is the xml:

JavaScript

I can make the other changes I need, but can’t find out how to remove the namespace and prefix. This is the reusklt xml I need:

JavaScript

And here is my script which will open and parse the xml and save it:

JavaScript

So how would I add code in my script which will remove the namespace and prefix?

Advertisement

Answer

Replace tag as Uku Loskit suggests. In addition to that, use lxml.objectify.deannotate.

JavaScript

Note: Some tags like Comment return a function when accessing tag attribute. added a guard for that.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement