Nokogiri: Select content between element A and B

前端 未结 3 970
误落风尘
误落风尘 2020-12-15 01:40

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

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 02:41

    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")
    

提交回复
热议问题