python lxml添加未使用的名称空间 [英] python lxml adds unused namespaces

查看:66
本文介绍了python lxml添加未使用的名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用lxml的find()方法在xml文件中选择节点时遇到问题.本质上,我正在尝试将节点从一个xml文件移动到另一个.

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.

文件1:

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

一旦我解析了文件1并对其进行了查找:

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

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

节点看起来像这样:

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

注意,它已将在文档中找到的名称空间添加到该节点.但是,该节点中没有任何一个使用这些名称空间.我将如何处理A)不编写所选节点中未使用的名称空间,或B)删除未使用的名称空间声明?如果在选定的节点中使用了它,那么我将需要它,但是否则,我想摆脱它们.有任何想法吗?谢谢!

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!

推荐答案

如果名称空间在文档中,则文档将使用名称空间.这些节点中使用了名称空间,因为这些节点是声明该名称空间的子树的一部分.按照Daenyth给出的链接删除它们,或在将XML字符串变成lxml对象之前将其剥离.

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.

这篇关于python lxml添加未使用的名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆