使用节点选择祖父母来选择其后代之一
我有一个与此类似的树:
tr
_|_
td td
_| |_
div div
_| |_
input "foobar"
我使用以下方法选择了节点“foobar”:
//*[normalize-space(text())='foobar']
但我无法选择曾祖父母tr
并用它选择曾孙input
有什么建议吗?
I have a tree similar to this:
tr
_|_
td td
_| |_
div div
_| |_
input "foobar"
I have the node "foobar' selected using:
//*[normalize-space(text())='foobar']
But I am unable to select the great grandparent tr
and select great grand child input
with it. Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜想您想要选择字符串值(规范化后)为“foobar”的元素,从顶部元素
tr
开始。下面是一个从顶部元素
tr
开始并选择所需元素的 XPath 表达式:如果您需要选择文本节点本身(而不是其元素父级):
如果您想避免代价高昂的操作并且缓慢评估
//
缩写(因为您知道文档结构),请使用以下表达式:甚至:
更新:
现在OP改变了他的问题并想要选择
div
中具有规范化字符串值“foobar”的input
元素。这是一个精确选择此内容的 XPath 表达式:
或
请注意:如果这些表达式未选择任何节点,则该文档很可能具有默认名称空间 - - 搜索 XPath 默认命名空间——这个问题有很多好的答案——在 SO 和互联网上。
I guess that you want to select the element whose string value (after normalization) is "foobar", starting from the top element
tr
.Here is one XPath expression that "starts" from the top elemet
tr
and selects the wanted element:If you need to select the text node itself (not its element parent):
If you want to avoid the costly and slow evaluation of the
//
abbreviation (because you know the document structure), use this expression:or even:
Update:
Now the OP changed his question and wants to select the
input
element from thediv
that has normalized string-value 'foobar'.Here is an XPath expression selecting exactly this:
or
Do note: In case these expressions don't select any node, it is most likely that the document has a default namespace -- search for XPath default namespace -- there are many good answers to this problem -- here at SO and on the internet.
此 XPath 表达式选择在空格规范化后具有等于“foobar”的后代文本节点的每个元素:
This XPath expression select every element having a descendant text node equal to "foobar" after whitespace normalization: