Python lxml adds unused namespaces

I had a problem when using the lxml find () method to select a node in an XML file. Essentially, I'm trying to move a node from one XML file to another.

File 1:

<somexml xmlns:a='...' xmlns:b='...' xmlns:c='...'> <somenode id='foo'> <something>bar</something> </somenode> </somexml> 

As soon as I analyze file 1 and find it:

 node = tree.find('//*[@id="foo"]') 

Node is as follows:

 <somenode xmlns:a='...' xmlns:b='...' xmlns:c='...'> <something>bar</something> </somenode> 

Notice that the namespaces found in the document are added to the node. However, nothing in this node uses any of these namespaces. How can I get around either A) not writing namespaces that are not used in the selected node, or B) deleting unused namespace names? If it is used in the selected node, then I will need it, but otherwise I would like to get rid of them. Any ideas? Thanks!

+1
source share
1 answer

If the namespaces are in the document, then the document uses namespaces. Namespaces are used in these nodes because these nodes are part of the subtree that declared the namespace. Follow the Daenyth link to remove them, or remove them from the XML string before turning it into an lxml object.

+3
source

Source: https://habr.com/ru/post/909582/


All Articles