在 XPath 中测试 text() 节点与字符串值 [英] Testing text() nodes vs string values in XPath
问题描述
我有一个节点如下:
<span class="portal-text-medium">Office Hours</span>
对于我使用的 XPath
For the XPath I use
//span[text()='Office Hours']
这应该有效,但它从来没有.我可以使用 *contains(text(),'Office Hours')]*
但这不会找到完全匹配的,我必须验证没有*".这不是唯一一次它对我不起作用.我以前见过它工作,所以我不知道出了什么问题.有什么想法吗?
which should work, but it never does. I can use *contains(text(),'Office Hours')]*
but that won't find an exact match and I have to verify there is no "*". This is not the only time it hasn't worked for me. I have seen it work before so I don't know what is wrong. Any idea?
是的,我可以并且确实使用 starts-with
但它并不完全相同.
Yes I can, and do, use starts-with
but it is not quite the same.
推荐答案
XPath text() =
与 XPath 不同.=
(匹配文本节点不同于匹配字符串值)
XPath text() =
is different than XPath . =
(Matching text nodes is different than matching string values)
以下 XPath 不相同...
The following XPaths are not the same...
//span[text() = '办公时间']
说:
选择 span
元素有一个直接孩子文本节点 等于办公时间".
Select the span
elements that
have an immediate child text node equal to 'Office Hours`.
//span[.= '办公时间']
说:
选择字符串值 等于办公时间".
Select the span
elements whose string value is equal to 'Office Hours`.
简而言之,对于元素节点:
字符串值元素节点是元素的串联所有文本节点的string-value元素的后代按文档顺序排列的节点.
The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.
示例
以下 span
元素只匹配 #1:
Examples
The following span
elements would match only #1:
<span class="portal-text-medium">办公时间<br/>8:00-10:00</span>
<span class="portal-text-medium">我的<br/>办公时间</span>
以下 span
元素只匹配 #2:
The following span
elements would match only #2:
<span class="portal-text-medium"><b>Office</b>营业时间
<span class="portal-text-medium"><b><i>办公时间</i></b></span>
莉>
以下 span
元素将匹配 #1 和 #2:
The following span
element would match both #1 and #2:
办公时间
这篇关于在 XPath 中测试 text() 节点与字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!