我想做的事情就是读取一个本地的.xml文件(将其编码为UTF-8以便它具有正确的标题,并重新保存文件)。然而,当我运行以下代码时,它会在每个XML元素中添加可怕的“ns0:”声明:
import xml.etree.ElementTree as ET
import sys, os
# note that this is the *module*'s `register_namespace()` function
# WTF THIS SHOULD WORK....
ET.register_namespace("", "http://www.w3.org/2000/svg")
tree = ET.ElementTree() # instantiate an object of *class* `ElementTree`
tree.parse('//cbweb1/inetpub/x/sitemap/sitemap_index.xml')
tree.write('//cbweb1/inetpub/x/sitemap/test.xml', encoding = 'utf-8', xml_declaration=True)
我做错了什么??
顺便说一下,这是Python 2.7.x(已尝试使用3.4)
编辑:
输入:
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/something.xml</loc>
<lastmod>2014-05-01</lastmod>
</sitemap>
</sitemapindex>
输出:
<?xml version="1.0" encoding="utf-8"?>
<ns0:sitemapindex xmlns:ns0="http://www.sitemaps.org/schemas/sitemap/0.9">
<ns0:sitemap>
<ns0:loc>http://www.example.com/something.xml</ns0:loc>
<ns0:lastmod>2014-05-01</ns0:lastmod>
</ns0:sitemap>
</ns0:sitemapindex>
http://www.sitemaps.org/schemas/sitemap/0.9
,为什么要将其设置为http://www.w3.org/2000/svg
?难道不应该使用ET.register_namespace("", "http://www.sitemaps.org/schemas/sitemap/0.9")
吗? - Joshua Taylor