Linked Questions

Popular Questions

XML Parsing with Multiple Namespaces

Asked by At

I am trying to parse and write XML using Python's standard library for xml parsing.

The format of the xml is as follows:

xml = '<class:Classification xmlns:class="schema:SpeciesClassification:2.0" xmlns="http://www.w3.org/1999/xhtml" dateClassified="2019-02-11" endangeredMarking="false" caveat="false"></class:Classification>'

When I parse this xml then push it back to a string, I get something strange. All the class: become ns0. How do I keep these markings?


from xml.etree.cElementTree import Element, SubElement, parse, tostring, fromstring

print(tostring(fromstring(xml)))
b'<ns0:Classification xmlns:ns0="schema:SpeciesClassification:2.0" caveat="false" dateClassified="2019-02-11" endangeredMarking="false" />'

Do I have to specify a different parser? I'm just a bit lost on why this gets dropped.

Thanks

Related Questions