and
wi...">

XPath only matches directly the following siblings

I have the following kind of HTML. Content is grouped by <div "id=foo">and <div "id=foo1">with elements <div "style=padding…">.

I am trying to figure out how to create an XPath expression that will allow me to disable "id=foo"in order to return sibling <div>with"style=padding…"

Getting <div id="foo">trivial. However, I can’t just do following-siblingbased on "style=padding…", because it returns all the corresponding <div>s.

I need a way to return a match <div>until I hit a brother that matches "id=foo1". I am pretty sure that there is a simple approach that I am missing!

<div id="foo">stuff...</div>

<div style="padding:2px; ">stuff...</div>

<div id="foo1">stuff...</div>

<div id="foo">stuff...</div>

<div style="padding:2px; ">stuff...</div>
<div style="padding:2px; ">stuff...</div>
<div style="padding:2px; ">stuff...</div>

<div id="foo1">stuff...</div>
+3
source share
5 answers

- , div, ?

div[not(@id)]

, , div ?

div[@style]

- , - , :

div[@style][following-sibling::div[@id='foo1']]

div , div, . , ?

, HTML , , , XPath, , . , , .

+5

, , , , : ( , , <div> id!):

/*/div[@id='foo'][n]/following-sibling::div[@style='padding…']
[
  count(preceding-sibling::div[@id='foo']) 
  =
  count(/*/div[@id='foo'][n]/preceding-sibling::div[@id='foo']) + 1
]

XPath <div style="padding…">, n'th <div id="foo"> ( , ).

-sibling <div id="foo"> , , . <div id="foo">, <div id="foo">. n, .

, :

//div[@style='padding…'][preceding-sibling::div[@id][1]/@id = 'foo']

<div style="padding…">, <div> ( id) id 'foo'. , , <div> 'foo', <div> .

+1

, XPath. , div ( ), # foo1 div, . , , XPath. XPath .

divs, . , XPath.

XPath, , divs ( , ), - div . , HTML, , XPath.

0

- HTML. .

-2

,

-3

Source: https://habr.com/ru/post/1727247/


All Articles