The following XPath is usually sufficient for matching all anchors whose text contains a certain string:
//a[contains(text(), 'SENIOR ASSOCIATES')]
Given a case like this though:
<a href="http://www.freshminds.net/job/senior-associate/"><strong>
SENIOR ASSOCIATES <br>
</strong></a>
The text is wrapped in a <strong>
, also there's also a <br>
before the anchor closes, and so the above XPath returns nothing.
How can the XPath be adapted so that it allows for the <a>
containing additional tags such as <strong>
, <i>
, <b>
, <br>
etc. while still working in the standard case?
答案 0 :(得分:2)
请勿使用text()
。
//a[contains(., 'SENIOR ASSOCIATES')]
与您的想法相反,text()
没有为您提供元素的文字。
这是一个节点测试,即一个选择实际节点列表(!)的表达式,即元素的文本节点子节点。
下面:
<a href="http://www.freshminds.net/job/senior-associate/"><strong>
SENIOR ASSOCIATES <br>
</strong></a>
a
没有文字节点子节点。所有文本节点都是strong
的子节点。所以text()
给你零节点。
下面:
<a href="http://www.freshminds.net/job/senior-associate/"> <strong>
SENIOR ASSOCIATES <br>
</strong></a>
有a
的一个文本节点子项。它是空的(如&#34;仅空格&#34;)。
.
仅选择一个节点(上下文节点,<a>
本身)。
现在,contains()
期望字符串作为其参数。如果一个参数不是字符串,则首先转换为字符串。
将节点集(由1个或多个节点组成)转换为字符串是通过连接集合(*)中第一个节点的所有文本节点后代来完成的。因此,使用.
(或更明确的等效string(.)
)会使SENIOR ASSOCIATES
被一堆空格包围,因为XML中有一堆空格。
要摆脱那个空格,请使用normalize-space()
函数:
//a[contains(normalize-space(.), 'SENIOR ASSOCIATES')]
或更短,因为&#34;当前节点&#34;是此函数的默认值:
//a[contains(normalize-space(), 'SENIOR ASSOCIATES')]
(*)这就是为什么使用//a[contains(.//text(), 'SENIOR ASSOCIATES')]
会在上面两个样本中的第一个中起作用而不是在第二个样本中起作用的原因。