Xpath OR 通过文本节点

Xpath OR over text node

我想捕捉 td 标签内的文本,但只想要不在标签 <strong><b>.[=19 内的文本=]

例如使用b标签的情况

<td class="">
    <b>In Care Of Name</b>
     text that I want to catch
</td>

对于这种情况,我可以使用此 xpath 表达式检索文本

//td[starts-with(., "In Care Of Name")]//text()[not(ancestor::b)]

我得到了预期的结果:

text that I want to catch

或使用 strong

的情况
<td class="">
        <strong>In Care Of Name</strong>
         text that I want to catch
</td>

对于这种情况,我可以使用此 xpath 表达式检索文本

//td[starts-with(., "In Care Of Name")]//text()[not(parent::strong)]

我尝试使用以下表达式将这两个 xpath 连接到一个上:

//td[starts-with(., "In Care Of Name")]//text()[not(parent::strong) or not(ancestor::b)]

我得到了

In Care Of Name
text that I want to catch

事实上,我得到了两个文本元素,这不是我所期望的。

知道哪里出了问题。我需要改变方法来解决这个问题吗?

提前致谢。

这个 XPath,

//td[starts-with(., "In Care Of Name")]/text()

将 return 字符串值以 In Care Of Name:

开头的 td 的直接文本节点 children
text that I want to catch

您的 XML 变体涉及 bstrong children td

请参阅 了解有关 XPath 中文本节点和字符串值之间差异的更多详细信息。

您想要 not(A or B)(或者 not(A) and not(B))而不是 not(A) or not(B)