NulledPointer NulledPointer - 3 months ago 24
Python Question

Why does xml.etree.ElementTree.parse converts namespace elements in lowercase

I want to instruct ET.parse to leave the namespace case in XML as it is

import xml.etree.ElementTree as ET
tree = ET.parse("mimeTypes.rdf")
ET.dump(tree)
<rdf:RDF xmlns:ns1="http://home.netscape.com/NC-rdf#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description ns1:value="irc" rdf:about="urn:scheme:irc">
<ns1:handlerProp rdf:resource="urn:scheme:handler:irc" />
</rdf:Description>
<rdf:Description ns1:value="application/pdf" rdf:about="urn:mimetype:application/pdf">
<ns1:handlerProp rdf:resource="urn:mimetype:handler:application/pdf" />
</rdf:Description>


Original file looks like this:

<?xml version="1.0"?>
<RDF:RDF xmlns:NC="http://home.netscape.com/NC-rdf#"
xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<RDF:Description RDF:about="urn:scheme:irc"
NC:value="irc">
<NC:handlerProp RDF:resource="urn:scheme:handler:irc"/>
</RDF:Description>
<RDF:Description RDF:about="urn:mimetype:application/pdf"
NC:value="application/pdf">
<NC:handlerProp RDF:resource="urn:mimetype:handler:application/pdf"/>
</RDF:Description>

Answer

Apparently I had to register the namespaces used in the XML:

 import xml.etree.ElementTree as ET
 tree = ET.parse("mimeTypes.rdf")
 et.register_namespace('NC', 'http://home.netscape.com/NC-rdf#')
 et.register_namespace('RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
 tree.write("tmp.xml", xml_declaration=True, encoding='utf-8', method="xml")