xml_etree_elementtree.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:python-xml 作者: emusical 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号