如何从Python xml etree ElementTree中的根元素中删除属性 [英] How to remove attribute from root element in Python xml etree ElementTree

查看:468
本文介绍了如何从Python xml etree ElementTree中的根元素中删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件包含以下数据:

My file contains the following data:

原始文件:

<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>

预期:

<?xml version="1.0" encoding="UTF-8"?><urlset> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>

我使用etree解析文件,并且想从根元素 urlset中删除该属性

I use etree to parse the file and I want to remove the attribute from the root element 'urlset'

import xml.etree.ElementTree as ET

tree = ET.parse("/Users/hsyang/Downloads/VI-0-11-14-2016_20.xml")
root = tree.getroot()

print root.attrib
>> {}

root.attrib.pop("xmlns", None)

print root.attrib
>> {}
ET.tostring(root)

我以为我应该得到{xmlns : http://www.sitemaps.org/schemas/sitemap/0.9 当我第一次打印root.attrib时,却得到了一个空字典。有人可以帮忙吗?

I thought I was supposed to get {xmlns:"http://www.sitemaps.org/schemas/sitemap/0.9"} when i print root.attrib the first time but I got an empty dictionary. Can someone help?

感谢!

推荐答案

xmlns = http://www.sitemaps.org/schemas/sitemap/0.9 看起来像常规属性,但这是特例,即名称空间声明。

xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" looks like a regular attribute but it is a special case, namely a namespace declaration.

删除,添加或修改名称空间可能非常困难。 常规属性存储在元素的可写 attrib 属性中。另一方面,命名空间映射不能通过API获得(在lxml库中,元素确实具有 nsmap 属性,但它是只读的)。

Removing, adding, or modifying namespaces can be quite hard. "Normal" attributes are stored in an element's writable attrib property. Namespace mappings on the other hand are not readily available via the API (in the lxml library, elements do have a nsmap property, but it is read-only).

我建议进行简单的文本搜索和替换操作,类似于对修改的答案具有lxml 的给定xml文档中的名称空间。像这样的东西:

I suggest a simple textual search-and-replace operation, similar to the answer to Modify namespaces in a given xml document with lxml. Something like this:

with open("input.xml", "r") as infile, open("output.xml", "w") as outfile:
    data = infile.read()
    data = data.replace(' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"', '')
    outfile.write(data)

另请参见如何使用Python将名称空间和前缀插入XML字符串?

这篇关于如何从Python xml etree ElementTree中的根元素中删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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