python lxml adds unused namespaces

我的未来我决定 提交于 2019-12-08 05:47:35

问题


I'm having an issue when using lxml's find() method to select a node in an xml file. Essentially I am 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>

Once I parse File 1 and do a find on it:

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

Node looks like this:

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

Notice it added the namespaces that were found in the document to that node. However, nothing in that node uses any of those namespaces. How would I go about either A) not writing namespaces that aren't used in the selected node, or B) removing unused name space declarations? If it's being used in the selected node then I will need it, but otherwise, I would like to get rid of them. Any ideas? Thanks!


回答1:


If the namespaces are in the document, then the document uses the namespaces. The namespaces are being used in those nodes, because those nodes are part of the subtree which declared the namespace. Follow the link given by Daenyth to remove them, or strip them off the XML string before you turn it into an lxml object.



来源:https://stackoverflow.com/questions/6365422/python-lxml-adds-unused-namespaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!