What\'s the smartest way to have Nokogiri select all content between the start and the stop element (including start-/stop-element)?
Check example code below to unde
For the sake of completeness a XPath only solution :)
It builds an intersection of two sets, the following siblings of the start element and the preceding siblings of the end element.
Basically you can build an intersection with:
$a[count(.|$b) = count($b)]
A little divided on variables for readability:
@start_element = "//p[@id='para-3']"
@end_element = "//p[@id='para-7']"
@set_a = "#@start_element/following-sibling::*"
@set_b = "#@end_element/preceding-sibling::*"
@my_content = value.xpath("#@set_a[ count(.|#@set_b) = count(#@set_b) ]
| #@start_element | #@end_element")
Siblings don't include the element itself, so the start and end elements must be included in the expression separately.
Edit: Easier solution:
@start_element = "p[@id='para-3']"
@end_element = "p[@id='para-7']"
@my_content = value.xpath("//*[preceding-sibling::#@start_element and
following-sibling::#@end_element]
| //#@start_element | //#@end_element")