莫回无
注对于Python的ElementTree标准库来说,这是一个有用的答案,而不需要使用硬编码的命名空间。要从XML数据中提取命名空间的前缀和URI,可以使用ElementTree.iterparse函数,只解析命名空间启动事件(起点):>>> from io import StringIO>>> from xml.etree import ElementTree>>> my_schema = u'''... ... ... basketball league... ... a group of sports teams that compete against each other... in Basketball... ... ... ... '''>>> my_namespaces = dict([... node for _, node in ElementTree.iterparse(... StringIO(my_schema), events=['start-ns']... )... ])>>> from pprint import pprint>>> pprint(my_namespaces){'': 'http://dbpedia.org/ontology/', 'owl': 'http://www.w3.org/2002/07/owl#', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'xsd': 'http://www.w3.org/2001/XMLSchema#'}然后,可以将字典作为参数传递给搜索函数:root.findall('owl:Class', my_namespaces)