我正在更新xml文件,并希望使用ET.register_namespace保留具有相同URI但不同锚标记的多个命名空间。
下面的代码是我尝试过的:
ET.register_namespace('', "http://oval.mitre.org/XMLSchema/oval-definitions-5")
ET.register_namespace('', "http://oval.mitre.org/XMLSchema/oval-definitions-5#windows")
ET.register_namespace('', "http://oval.mitre.org/XMLSchema/oval-definitions-5#independent")
ns = "{http://oval.mitre.org/XMLSchema/oval-definitions-5}"
f = open ("def_ex.xml","ra")
tree = ET.parse(f)
root = tree.getroot()
for defn in root.iter('%stag' %ns):
if "patch" in defn.get("class"): #pick id attrib where class attrib is "patch"
print defn.get("id")
mirr_def = copy.deepcopy(defn)
defn.append(mirr_def)
tree.write("def_ex.xml")
exit()但问题是,third名称空间正在覆盖one和two,如下代码的输出所示:
<ns0:tag>
.......
.......
</ns0:tag>
<ns1:tag1>
........
........
</ns1:tag1>
<tag2>
......
......
</tag2>我的最后一个问题是,当具有相同URI的不同“锚标记”时,如何保留所有名称空间而不相互覆盖?
更新: def_ex.xml
<oval_definitions xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oval="http://oval.mitre.org/XMLSchema/oval-common-5" xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5" xmlns:windows-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#windows" xmlns:independent-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" xsi:schemaLocation=" http://oval.mitre.org/XMLSchema/oval-definitions-5#windows windows-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5#independent independent-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-definitions-5 oval-definitions-schema.xsd http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd">
<tag id="oval:def:1" class="inventory">
...........
...........
...........
</tag>
<tag1 xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#windows" id="oval:tst:1" version="1">
............
............
</tag1>
<tag2 xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent" id="oval:tst:2" version="1">
............
............
</tag2>
</oval_definitions>发布于 2014-09-08 09:18:35
正如@mu無所说,您将无法使用明确防止重复前缀的register_namespace来实现您想要的结果。
我不确定您要做的是合法的XML,还是由库支持的,但是实现您想要的结果的一种方法是直接实现行为 of register_namespace:
xml.etree.ElementTree._namespace_map[uri] = prefix # Replace uri and prefix.并作为一个函数(从原始python库源代码修改):
import re
import xml.etree.ElementTree
def register_namespace(prefix, uri):
if re.match("ns\d+$", prefix):
raise ValueError("Prefix format reserved for internal use")
xml.etree.ElementTree._namespace_map[uri] = prefix我做了,而不是建议这样做,因为它可能以意想不到的方式破坏其他地方的库。
免责声明:我的代码还没有经过测试。
发布于 2014-09-01 12:29:45
您使用相同的前缀来定义所有的3个URI。正如文档中所提到的,命名空间注册中心是全局的,因此值被覆盖。
来自文档
xml.etree.ElementTree.register_namespace(prefix, uri)注册名称空间前缀。注册表是全局的,对于给定前缀或命名空间URI的任何现有映射都将被删除。前缀是命名空间前缀。uri是一个命名空间uri。如果可能的话,此命名空间中的标记和属性将使用给定的前缀序列化。
我建议您为每个URI添加名称空间如下,并相应地使用它们
namespaces = {'ns1': 'http://oval.mitre.org/XMLSchema/oval-definitions-5',
'ns2': 'http://oval.mitre.org/XMLSchema/oval-definitions-5#windows',
'ns3': 'http://oval.mitre.org/XMLSchema/oval-definitions-5#independent'}
for prefix, uri in namespaces.items():
ET.register_namespace(prefix, uri)https://stackoverflow.com/questions/25544139
复制相似问题