4

Nokogiri に start 要素と stop 要素 (start-/stop-要素を含む) の間のすべてのコンテンツを選択させる最もスマートな方法は何ですか?

私が探しているものを理解するために、以下のコード例を確認してください。

require 'rubygems'
require 'nokogiri'

value = Nokogiri::HTML.parse(<<-HTML_END)
  "<html>
    <body>
      <p id='para-1'>A</p>
      <div class='block' id='X1'>
        <p class="this">Foo</p>
        <p id='para-2'>B</p>
      </div>
      <p id='para-3'>C</p>
      <p class="that">Bar</p>
      <p id='para-4'>D</p>
      <p id='para-5'>E</p>
      <div class='block' id='X2'>
        <p id='para-6'>F</p>
      </div>
      <p id='para-7'>F</p>
      <p id='para-8'>G</p>
    </body>
  </html>"
HTML_END

parent = value.css('body').first

# START element
@start_element = parent.at('p#para-3')
# STOP element
@end_element = parent.at('p#para-7')

結果 (戻り値) は次のようになります

<p id='para-3'>C</p>
<p class="that">Bar</p>
<p id='para-4'>D</p>
<p id='para-5'>E</p>
<div class='block' id='X2'>
  <p id='para-6'>F</p>
</div>
<p id='para-7'>F</p>

更新:これは私の現在のソリューションですが、もっとスマートなものが必要だと思います:

@my_content = ""
@selected_node = true

def collect_content(_start)

  if _start == @end_element
    @my_content << _start.to_html
    @selected_node = false
  end

  if @selected_node == true
    @my_content << _start.to_html
    collect_content(_start.next)
  end

end

collect_content(@start_element)

puts @my_content
4

3 に答える 3

10

再帰を使用する非常にスマートなワンライナー:

def collect_between(first, last)
  first == last ? [first] : [first, *collect_between(first.next, last)]
end

反復ソリューション:

def collect_between(first, last)
  result = [first]
  until first == last
    first = first.next
    result << first
  end
  result
end

編集:(短い)アスタリスクの説明

これはスプラット演算子と呼ばれます。配列を「展開」します。

array = [3, 2, 1]
[4, array]  # => [4, [3, 2, 1]]
[4, *array] # => [4, 3, 2, 1]

some_method(array)  # => some_method([3, 2, 1])
some_method(*array) # => some_method(3, 2, 1)

def other_method(*array); array; end
other_method(1, 2, 3) # => [1, 2, 3] 
于 2009-05-04T16:20:10.263 に答える
2
# monkeypatches for Nokogiri::NodeSet
# note: versions of these functions will be in Nokogiri 1.3
class Nokogiri::XML::NodeSet
  unless method_defined?(:index)
    def index(node)
      each_with_index { |member, j| return j if member == node }
    end
  end

  unless method_defined?(:slice)
    def slice(start, length)
      new_set = Nokogiri::XML::NodeSet.new(self.document)
      length.times { |offset| new_set << self[start + offset] }
      new_set
    end
  end
end

#
#  solution #1: picking elements out of node children
#  NOTE that this will also include whitespacy text nodes between the <p> elements.
#
possible_matches = parent.children
start_index = possible_matches.index(@start_element)
stop_index = possible_matches.index(@end_element)
answer_1 = possible_matches.slice(start_index, stop_index - start_index + 1)

#
#  solution #2: picking elements out of a NodeSet
#  this will only include elements, not text nodes.
#
possible_matches = value.xpath("//body/*")
start_index = possible_matches.index(@start_element)
stop_index = possible_matches.index(@end_element)
answer_2 = possible_matches.slice(start_index, stop_index - start_index + 1)
于 2009-05-07T11:09:29.843 に答える
2

完全を期すために、XPath のみのソリューション :)
開始要素の次の兄弟と終了要素の前の兄弟の 2 つのセットの交差を構築します。

基本的に、次のもので交差点を構築できます。

  $a[count(.|$b) = count($b)]

読みやすくするために、変数を少し分割します。

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

兄弟には要素自体が含まれないため、開始要素と終了要素を別々に式に含める必要があります。

編集:より簡単な解決策:

@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")
于 2009-09-27T21:06:10.330 に答える