All I'm trying to do is read a local .xml file (encode it UTF-8 so it has the proper header, and re-save the file). However, when I run the following, it adds the dreaded "ns0:" declaration within each XML element:
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)
What am I doing wrong??
FYI, this is Python 2.7.x (have tried with 3.4)
EDIT:
Input:
<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>
Output:
<?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>
If the default namespace in the original input is http://www.sitemaps.org/schemas/sitemap/0.9
, as shown in:
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
then if you set setting it to something else with
ET.register_namespace("", "http://www.w3.org/2000/svg")
you'll need some other namespace declared in the output for http://www.sitemaps.org/schemas/sitemap/0.9
. Instead, you should set it to the same value:
ET.register_namespace("", "http://www.sitemaps.org/schemas/sitemap/0.9")
(Alternatively, you might try simply not setting it to anything at all; perhaps the same namespaces would be used by default in the output that were present in the input.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With