def lookup_ns_prefix_for_uri(self, node, uri):
if uri == nodes.Node.XMLNS_URI:
return 'xmlns'
result = None
# Lookup namespace URI in ET's awful global namespace/prefix registry
if hasattr(BaseET, '_namespace_map') and uri in BaseET._namespace_map:
result = BaseET._namespace_map[uri]
if result == '':
result = None
if result is None or re.match('ns\d', result):
# We either have no namespace prefix in the global mapping, in
# which case we will try looking for a matching xmlns attribute,
# or we have a namespace prefix that was probably assigned
# automatically by ElementTree and we'd rather use a
# human-assigned prefix if available.
curr_node = node
while self._is_node_an_element(curr_node):
for n, v in curr_node.attrib.items():
if v == uri:
if n.startswith('xmlns:'):
result = n.split(':')[1]
return result
elif n.startswith('{%s}' % nodes.Node.XMLNS_URI):
result = n.split('}')[1]
return result
curr_node = self.get_node_parent(curr_node)
return result
评论列表
文章目录