Is there a key for the default namespace when creating dictionary for use with xml.etree.ElementTree.findall() in Python?

别等时光非礼了梦想. 提交于 2019-12-06 10:55:12

Decided to take a look at the source code for the method. So as it turns out, the following code in xml.etree.ElementPath is doing the dirty work:

def xpath_tokenizer(pattern, namespaces=None):
    for token in xpath_tokenizer_re.findall(pattern):
        tag = token[1]
        if tag and tag[0] != "{" and ":" in tag:
            try:
                prefix, uri = tag.split(":", 1)
                if not namespaces:
                    raise KeyError
                yield token[0], "{%s}%s" % (namespaces[prefix], uri)
            except KeyError:
                raise SyntaxError("prefix %r not found in prefix map" % prefix)
        else:
            yield token

pattern is the tag passed into findall(). If there is no : found in the tag, the tokenizer simply returns the tag back without any substitutions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!