如何强制ElementTree将xmlns属性保留在其原始元素中? [英] How to force ElementTree to keep xmlns attribute within its original element?

查看:39
本文介绍了如何强制ElementTree将xmlns属性保留在其原始元素中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入XML文件:

 <?xml version ='1.0'encoding ='utf-8'?><配置><运行时名称="test"版本="1.2" xmlns:ns0 ="urn:schemas-microsoft-com:asm.v1">< ns0:assemblyBinding>< ns0:dependentAssembly/></ns0:assemblyBinding></runtime></configuration> 

...和Python脚本:

 将xml.etree.ElementTree导入为ETfile_xml ='test.xml'树= ET.parse(file_xml)根= tree.getroot()打印(root.tag)打印(root.attrib)element_runtime = root.find('.//runtime')打印(element_runtime.tag)打印(element_runtime.attrib)tree.write(file_xml,xml_declaration = True,encoding ='utf-8',method ="xml") 

...给出以下输出:

 > test.py配置{}运行{'name':'test','version':'1.2'} 

...并且具有将XML修改为以下内容的不必要的副作用:

 <?xml version ='1.0'encoding ='utf-8'?><配置xmlns:ns0 ="urn:schemas-microsoft-com:asm.v1"><运行时名称=测试"版本="1.2">< ns0:assemblyBinding>< ns0:dependentAssembly/></ns0:assemblyBinding></runtime></configuration> 

我的原始脚本修改了XML,因此我必须调用 tree.write 并保存编辑后的文件.但是问题是ElementTree解析器将 xmlns 属性从 runtime 元素移动到根元素 configuration ,这在我的情况下是不希望的.

我无法从根元素中删除 xmlns 属性(将其从其属性的字典中删除),因为该属性未在其属性列表中列出(与为列出的属性不同)> runtime 元素).

为什么xmlns属性永远不会在任何元素的属性列表中列出?

如何强制ElementTree将xmlns属性保留在其原始元素内?

我正在Windows上使用Python 3.5.1.

解决方案

xml.etree.ElementTree 将所有名称空间拉入第一个元素,因为它在内部不跟踪声明该名称空间的元素

如果您不想这样做,则必须编写自己的序列化逻辑.

更好的选择是使用 lxml 而不是 xml.etree,因为它保留了声明名称空间前缀的位置.

I have an input XML file:

<?xml version='1.0' encoding='utf-8'?>
<configuration>
  <runtime name="test" version="1.2" xmlns:ns0="urn:schemas-microsoft-com:asm.v1">
    <ns0:assemblyBinding>
      <ns0:dependentAssembly />
    </ns0:assemblyBinding>
  </runtime>
</configuration>

...and Python script:

import xml.etree.ElementTree as ET

file_xml = 'test.xml'

tree = ET.parse(file_xml)
root = tree.getroot()
print (root.tag)
print (root.attrib)

element_runtime = root.find('.//runtime')
print (element_runtime.tag)
print (element_runtime.attrib)

tree.write(file_xml, xml_declaration=True, encoding='utf-8', method="xml")

...which gives the following output:

>test.py
configuration
{}
runtime
{'name': 'test', 'version': '1.2'}

...and has an undesirable side-effect of modifying XML into:

<?xml version='1.0' encoding='utf-8'?>
<configuration xmlns:ns0="urn:schemas-microsoft-com:asm.v1">
  <runtime name="test" version="1.2">
    <ns0:assemblyBinding>
      <ns0:dependentAssembly />
    </ns0:assemblyBinding>
  </runtime>
</configuration> 

My original script modifies XML so I do have to call tree.write and save edited file. But the problem is that ElementTree parser moves xmlns attribute from runtime element up to the root element configuration which is not desirable in my case.

I can't remove xmlns attribute from the root element (remove it from the dictionary of its attributes) as it is not listed in a list of its attributes (unlike the attributes listed for runtime element).

Why does xmlns attribute never gets listed within the list of attributes for any element?

How to force ElementTree to keep xmlns attribute within its original element?

I am using Python 3.5.1 on Windows.

解决方案

xml.etree.ElementTree pulls all namespaces into the first element as it internally doesn't track on which element the namespace was declared originally.

If you don't want that, you'll have to write your own serialisation logic.

The better alternative would be to use lxml instead of xml.etree, because it preserves the location where a namespace prefix is declared.

这篇关于如何强制ElementTree将xmlns属性保留在其原始元素中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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