我正在尝试使用python中的xml.etree创建shibboleth配置文件,并且在输出完成的文档时我忽略了命名空间分配的问题。 我很确定这是Outputting an “unused” XML namespace using ElementTree
中描述的问题我宣布他们......
namespaces = {
'resolver': 'urn:mace:shibboleth:2.0:resolver',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'pc': 'urn:mace:shibboleth:2.0:resolver:pc',
'ad': 'urn:mace:shibboleth:2.0:resolver:ad',
'dc': 'urn:mace:shibboleth:2.0:resolver:dc',
'enc': 'urn:mace:shibboleth:2.0:attribute:encoder',
'sec': 'urn:mace:shibboleth:2.0:security',
}
for prefix, uri in namespaces.iteritems():
ET.register_namespace(prefix, uri)
如果我打印出为文档定义的名称空间......
print json.dumps( ET._namespace_map, indent=True )
我在那里看到我的命名空间..
{
"urn:mace:shibboleth:2.0:resolver:pc": "pc",
"http://schemas.xmlsoap.org/wsdl/": "wsdl",
"urn:mace:shibboleth:2.0:resolver:ad": "ad",
"urn:mace:shibboleth:2.0:resolver": "resolver",
"http://www.w3.org/1999/xhtml": "html",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
"urn:mace:shibboleth:2.0:security": "sec",
"urn:mace:shibboleth:2.0:attribute:encoder": "enc",
"http://www.w3.org/2001/XMLSchema": "xs",
"http://www.w3.org/2001/XMLSchema-instance": "xsi",
"http://www.w3.org/XML/1998/namespace": "xml",
"urn:mace:shibboleth:2.0:resolver:dc": "dc"
}
当我将文档写入文件或将其转储到stdout时,它会省略“enc”和“pc”命名空间。与其他问题一样,“enc”和“pc”不用于限定元素,但用于限定属性值。我可以通过手动将它们添加到文档来强制它们出现......
root.set("xmlns:enc", "urn:mace:shibboleth:2.0:attribute:encoder")
root.set("xmlns:pc", "urn:mace:shibboleth:2.0:resolver:pc")
感觉我不应该 来做到这一点。 有没有办法在输出文档时强制xml.etree输出所有已定义的命名空间?
利安