相关疑难解决方法(0)

强制 xml.etree 输出“未使用”的命名空间

我正在尝试在 python 中使用 xml.etree 创建 shibboleth 配置文件,但我在输出完成的文档时遇到了省略命名空间分配的问题。我很确定这是使用 ElementTree 输出“未使用的”XML 命名空间中描述的问题

我宣布他们...

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)
Run Code Online (Sandbox Code Playgroud)

如果我打印出为文档定义的命名空间...

print json.dumps( ET._namespace_map, indent=True )
Run Code Online (Sandbox Code Playgroud)

我在那里看到我的命名空间..

{
 "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"
}
Run Code Online (Sandbox Code Playgroud)

当我将文档写入文件或将其转储到标准输出时,它会忽略“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")
Run Code Online (Sandbox Code Playgroud)

这感觉就像我不应该这样做。有没有办法在输出文档时强制 xml.etree …

python xml elementtree shibboleth xml-namespaces

8
推荐指数
0
解决办法
505
查看次数

标签 统计

elementtree ×1

python ×1

shibboleth ×1

xml ×1

xml-namespaces ×1