Remove ns0 from XML

后端 未结 2 960
栀梦
栀梦 2020-12-20 13:59

I have an XML file where I would like to edit certain attributes. I am able to properly edit the attributes but when I write the changes to the file, the tags have a strange

相关标签:
2条回答
  • 2020-12-20 14:33

    Actually the way to do it seemed to be a combination of two things.

    1. The import statement is import xml.etree.ElementTree as ET
    2. ET.register_namespace("", NAMESPACE) is the correct call, where NAMESPACE is the namespace listed in the input xml, ie the url after xmlns.
    0 讨论(0)
  • 2020-12-20 14:44

    Here's the corrected code using only xml.etree.ElementTree instead of lxml:

    import xml.etree.ElementTree as ET
    frag_xml_tree = ET.parse(xml_name)
    frag_root = frag_xml_tree.getroot()
    
    for e in frag_root: 
        for elem in frag_root.iter(e):
            elem.attrib[frag_param_name] = update_val
        ET.register_namespace("", "http://www.w3.org/2001")
        frag_xml_tree.write(xml_name)
    
    0 讨论(0)
提交回复
热议问题