没有名称空间的Python XpathEvaluator [英] Python XpathEvaluator without namespace
问题描述
我需要编写一个动态函数来查找ATOM xml文档的子树上的元素.
I need to write a dynamic function that finds elements on a subtree of an ATOM xml document.
为此,我写了这样的话:
To do so, I've written something like this:
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)
上面的代码无法找到类别.
The code above fails to find category.
原因是getpath返回的XPath没有名称空间,而XPathEvaluator e()需要名称空间.
The reason is that getpath returns an XPath without namespaces, whereas the XPathEvaluator e() requires namespaces.
是否有一种方法可以使getpath返回路径中的名称空间,或者允许XPathEvaluator接受路径而不指定名称空间(或者以其他方式指定它)?
Is there a way to either make getpath return namespaces in the path, or allow XPathEvaluator to accept the path without specifying the namespace (or, rather, specifying it some other way)?
推荐答案
使用:
*[local-name() = 'category']
Or, if you want to be more precise
:
*[local-name() = 'category' and namespace-uri() = 'http://www.w3.org/2005/Atom']
或者简单地:
def:category
这篇关于没有名称空间的Python XpathEvaluator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!