Grab following siblings in XPath?

谁都会走 提交于 2021-02-10 05:16:40

问题


I am trying to grab the li class text in the following based on the strong tag higher up in the tree containing a certain word, in this case: "restaurants":

<p class="">The location, where the condo is situated, 
            offers a good choice of <strong>restaurants</strong>.
            Some of them are listed below:</p>
<ul class="">
<li class="">Restaurant 1</li>
<li class="">Restaurant 2</li>
<li class="">Restaurant 3</li>
<li class="">Restaurant 4</li>
<li class="">Restaurant 5</li>
</ul>

I have tried many variations of the following, assuming that I need to go higher up the tree by using "ancestors":

//ul/li[contains(ancestor-or-self::@strong[contains(text(),'Restaurants')])

回答1:


This XPath,

//p[strong = 'restaurants']/following-sibling::ul[1]/li

will select all li elements of the first ul element following sibling of the p element that contains a strong element whose string value is "restaurants".



来源:https://stackoverflow.com/questions/45724510/grab-following-siblings-in-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!