tags:

views:

262

answers:

2

can anyone tell me if its possible to select only the divs 2a and 2b from this html fragment? the problem is that the divs are not children of h4 element and so the xpath query should say like "get the divs that are between the h4='Two' and the h4 that is right after h4='Two' note that i want the query to be dynamic and u tell her the start element (h4='Two') and the end element (any h4) and then on which filter to get the elements between.

<h4>One</h4>
<div>1a</div>
<div>1b</div>
<div>1c</div>
<h4>Two</h4>
<div>2a</div>
<div>2b</div>
<h4>Three</h4>
<div>3a</div>
<div>3b</div>
<div>3c</div>
+3  A: 
div[preceding-sibling::h4[1] = 'Two']
Pavel Minaev
i think this will get elements 3a,3b,3c too.i need only elements 2a and 2balso what does that [1] in the h4[1] means?
Karim
`preceding-sibling::h4[1]` means the _first_ preceding `h4`. Therefore, for 3a/3b/3c, it will be `<h4>Three</h4>`, which is why they won't appear in the result. Also note that this query will also select nodes "at the end" - i.e. those preceded `<h4>`, but not followed by one. If you want to check for that, adding `[... and following-sibling::h4]` to the filter should do the trick.
Pavel Minaev
A: 
div[preceding-sibling::h4='Two' and following-sibling::h4='Three']
Chris Persichetti
this will work but i need it to be generic.i mean its ok that the preceding sibling is searched by the Text 'Two'but i need the following sibbling to be any ending <h4> and no filtered by text
Karim