html - How can I select a dynamically limited range of nodes without using non-dynamic positions? -
<li></li> <th> <li></li> <strong>"jeff"<strong> <li></li> <a>"mary"<a> <li></li> <th> <li></li> <strong>"rich"<strong> <li></li> <strong>"alex"<strong> <li></li> <a>"bob"<a> <li></li> <th> <li></li> <strong>"dave"<strong> <li></li> <a>"joseph"<a> <li></li> <th>
i need select list items contain "jeff" , "mary" using "jeff" in xpath, , without selecting else , without using non-dynamic positions.
example:
//li[following-sibling::li/strong[contains(text() , "jeff")]]/following-sibling::li[th][1]/preceding-sibling::li[not(th)]
this works great first set(jeff , mary), once applied ranges further down code, falls apart , begins select more needed.
example:
//li[following-sibling::li/strong[contains(text() , "rich")]]/following-sibling::li[th][1]/preceding-sibling::li[not(th)]
this selects "rich", "alex", "bob"...but "jeff" , "mary"
example:
//li[following-sibling::li/strong[contains(text() , "dave")]]/following-sibling::li[th][1]/preceding-sibling::li[not(th)]
this selects "dave" , "joseph", "rich", "alex", "bob", "jeff" , "mary".
i want able plug in name, , have expression selects list items siblings of list item contains name, stops selecting once reaches list item contains header. problem seems lie in list items headers not unique in way. xpaths cannot do?
your sample html not well-formed xml i.e many elements missing closing tags. consider following well-formed xml sample adapted yours :
<div> <li/> <th> <li/> <strong>"jeff"</strong> <li/> <a>"mary"</a> <li/> </th> <li/> <strong>"rich"</strong> <li/> <strong>"alex"</strong> <li/> <a>"bob"</a> <li/> <th> <li/> <strong>"dave"</strong> <li/> <a>"joseph"</a> <li/> </th> </div>
the following xpath returned wanted list of text nodes 'jeff', 'rich', , 'dave' :
//li[ following-sibling::*[1][contains(text(),'put name here')] ]/following-sibling::*/text()
the xpath first looks <li>
directly followed element containing name. found <li>
, return text nodes within following sibling elements.
the xpath fail, however, if have set of names isn't wrapped in <th>
(currently have 1 set of such names, fine). can add logic counts preceding <th>
, example, handle case. make xpath complex though, won't implement unless needed.