首页 > 解决方案 > 具有以下特定兄弟情况的 XPath

问题描述

我的结构看起来像这样

<p>
   <br>
       <b>Text to fetch </b>
   <br>
       "Some random text"
       <b>Text not to fetch</b>

我需要 XPath,只有当br元素和他的下一个兄弟元素之间没有文本时,我才能获取br元素的下一个兄弟元素。

如果我做这样的事情

//br/following-sibling::b/text()[1]

它会同时获取Text to fetchand Text not to fetch,而我只需要Text to fetch.

标签: xpath

解决方案


尝试使用 XPath 来避免将b节点与前面的同级文本匹配:

//br/following-sibling::b[not(preceding-sibling::text()[1][normalize-space()])]/text()

推荐阅读