Remove ns0 from XML

放肆的年华 提交于 2020-07-20 07:28:51

问题


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 "ns0" added onto them. How can I get rid of this? This is what I have tried and have been unsuccessful. I am working in Python and using lxml.

    import xml.etree.ElementTree as ET
    from xml.etree import ElementTree as etree
    from lxml import etree, objectify
    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
        etree.register_namespace("", "http://www.w3.org/2001")
        frag_xml_tree.write(xml_name)

However, when I do this, I only get the error Invalid tag name u''. I thought this error came up if the xml tags started with digits but that is not the case with my xml. I am really stuck on how to proceed. Thanks


回答1:


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.



回答2:


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)


来源:https://stackoverflow.com/questions/38708658/remove-ns0-from-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!