从XML移除ns0 [英] Remove ns0 from XML

查看:90
本文介绍了从XML移除ns0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,我想在其中编辑某些属性.我能够正确地编辑属性,但是当我将更改写入文件时,标记上添加了一个奇怪的"ns0".我该如何摆脱呢?这是我尝试过但未成功的方法.我正在使用Python并使用lxml.

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)

但是,当我这样做时,我只会收到错误无效的标签名称u''.我认为如果xml标记以数字开头会出现此错误,但我的xml并非如此.我真的对如何继续深感困惑.谢谢

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

推荐答案

实际上,解决方案似乎是两件事的结合.

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

  1. 导入语句是将xml.etree.ElementTree导入为ET
  2. ET.register_namespace(",NAMESPACE)是正确的调用,其中NAMESPACE是输入xml中列出的名称空间,即xmlns之后的URL.

这篇关于从XML移除ns0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆