我需要编写一个动态函数,以查找ATOM xml文档子树上的元素。
为此,我已经编写了类似以下的代码:
tree = etree.parse(xmlFileUrl)
e = etree.XPathEvaluator(tree, namespaces={'def':'http://www.w3.org/2005/Atom'})
entries = e('//def:entry')
for entry in entries:
mypath = tree.getpath(entry) + "/category"
category = e(mypath)
上面的代码无法找到类别。
原因是getpath返回没有命名空间的XPath,而XPathEvaluator e()需要命名空间。
有没有办法使getpath返回带有命名空间的路径,或者允许XPathEvaluator接受未指定命名空间的路径(或以其他方式指定它)?