是否可以获取用于评估 xpath 结果的所有上下文节点?
是否可以获取用于评估 xpath 结果的所有上下文节点? 在下面的代码中:
test_xml = """
<r>
<a/>
<a>
<b/>
</a>
<a>
<b/>
</a>
</r>
"""
test_root = lxml.etree.fromstring(test_xml)
res = test_root.xpath("//following-sibling::*[1]/b")
for node in res:
print test_root.getroottree().getpath(node)
结果是:
/r/a[2]/b
/r/a[3]/b
是否可以获取上面 xpath 评估中使用的所有上下文节点:
/r/a[1] /r/a[2] /r/a[2]/b
以及第二个结果:
/r/a[2] /r/a[3]/b /r/a[3]/b
? 当使用子轴时,我可以让这些节点继续工作,
element_tree.getpath(elem)
但是其他轴呢?
TIA,问候
Is it possible get all context nodes used to evalute xpath result ?
In below code:
test_xml = """
<r>
<a/>
<a>
<b/>
</a>
<a>
<b/>
</a>
</r>
"""
test_root = lxml.etree.fromstring(test_xml)
res = test_root.xpath("//following-sibling::*[1]/b")
for node in res:
print test_root.getroottree().getpath(node)
Result are:
/r/a[2]/b
/r/a[3]/b
Is it possible to get all context nodes used in above xpath evaluation:
/r/a[1] /r/a[2] /r/a[2]/b
and for second result:
/r/a[2] /r/a[3]/b /r/a[3]/b
?
When using child axis I could get those nodes from working on
element_tree.getpath(elem)
but what abouth other axes ?
TIA, regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我反转了每个阶段的 xpath 以获取将在结果中评估哪些项目:
给我:
/r/a[2]/b、/r/a[2]、/r/a[1]、/r
/ r/a[3]/b、/r/a[3]、/r/a[2]、/r
I reversed the xpath for each stage to get which items would be evaluated in the result:
gives me:
/r/a[2]/b, /r/a[2], /r/a[1], /r
/r/a[3]/b, /r/a[3], /r/a[2], /r
如果我正确理解你的问题,那么你正在尝试获取特定节点的祖先节点。其 XPATH 表达式为:
例如:
If I am understanding your question correctly, you are trying to just get the ancestor nodes of particular nodes. The XPATH expression for this would be:
such as: