【发布时间】:2014-05-27 09:02:10
【问题描述】:
我正在使用以下代码访问 xml 文件中的属性值并将其删除
import lxml.etree as et
def ignore_xpath(xmlFile,xpath):
tree = et.parse(xmlFile)
for elt in tree.xpath(xpath):
elt=''
print et.tostring(tree, pretty_print=True, xml_declaration=True)
但这不适用于 xpath:/root/@attribute
我做错了什么,我怎么知道我的elt 是属性还是标签(case xpath= /root[@attribute])?我想以不同的方式对待他们。
【问题讨论】:
-
显示您的 xml。另外,如果您选择
/@attribute,您将获得一个属性列表,因此不需要额外查看它是标签还是属性。 -
感谢您的回答..我的 xml 文件并不特别我想要做的是,如果给定的 xpath 指示一个属性,我的函数应该用 '' 替换它的值:例如,如果我的 xml 文件就像:
和 xpath 是:/root/@hello 我想找到一种方法将其转换为 -
我的回答有帮助吗?如果是,请接受。如果不是,那是什么问题。