所以我有下面的html示例要解析。
<div>
<strong>Title:</strong>
Sub Editor at NEWS ABC
<strong>Name:</strong>
John
<strong>Where:</strong>
Everywhere
<strong>When:</strong>
Anytime
<strong>Everything can go down there..</strong>
Lorem Ipsum blah blah blah....
</div>
我想提取这个完整的div,除了我不想标题,在什么地方,什么时候用下面的值。
到目前为止,我已经测试了以下xpath。
a)没有以下兄弟姐妹(1:不工作。2:工程)
1. //div/node()[not(strong[contains(text(), "Title")])]
2. //div/node()[not(self::strong and contains(text(), "Title"))]
a)与以下兄弟姐妹(1:不工作。2:不工作)
1. //div/node()[not(strong[contains(text(), "Title")]) and not(strong[contains(text(), "Title")]/following-sibling::text())]
2. //div/node()[not(self::strong and contains(text(), "Title") and following-sibling::text())]
如何实现我所追求的?
最佳答案
我认为下面的代码符合您所要做的——它排除了包含title的强元素以及它后面的文本节点。可以将其展开以包含要排除的其他强元素:
//div/node()[not(self::strong and contains(text(), "Title") or preceding-sibling::strong[1][contains(text(), "Title")])]
强节点被跳过:
not(self::strong and contains(text(), "Title")
以下文本被跳过:
preceding-sibling::strong[1][contains(text(), "Title")]
请注意,文本节点需要检查其最近的前一个同级(而不是其后一个同级)。