在我的Rails应用程序中,我有如下所示的HTML,在Nokogiri中解析。
我希望能够选择HTML块。例如,如何使用XPath或CSS选择<sup id="21">
部分的HTML块?假设在真实的HTML中,********
的部分不存在。
我想通过<sup id=*>
拆分HTML,但问题是节点是兄弟姐妹。
<sup class="v" id="20">
1
</sup>
this is some random text
<p></p>
more random text
<sup class="footnote" value='fn1'>
[v]
</sup>
# ****************************** starting here
<sup class="v" id="21">
2
</sup>
now this is a different section
<p></p>
how do we keep this separate
<sup class="footnote" value='fn2'>
[x]
</sup>
# ****************************** ending here
<sup class="v" id="23">
3
</sup>
this is yet another different section
<p></p>
how do we keep this separate too
<sup class="footnote" value='fn3'>
[r]
</sup>
答案 0 :(得分:1)
您希望选择sup
与@id='21'
和sup
与@id='23'
之间的所有内容。使用以下特殊表达式:
//sup[@id='21']|(//sup[@id='21']/following-sibling::node()[
not(self::sup[@id='23'] or preceding-sibling::sup[@id='23'])])
或Kayessian节点集交集公式的应用:
//sup[@id='21']|(//sup[@id='21']/following-sibling::node()[
count(.|//sup[@id='23']/preceding-sibling::node())
=
count(//sup[@id='23']/preceding-sibling::node())])
答案 1 :(得分:1)
这是一个简单的解决方案,可为NodeSet
提供<sup … class="v">
之间的所有节点,并按其id
进行哈希处理。
doc = Nokogiri.HTML(your_html)
nodes_by_vsup_id = Hash.new{ |k,v| k[v]=Nokogiri::XML::NodeSet.new(doc) }
last_id = nil
doc.at('body').children.each do |n|
last_id = n['id'] if n['class']=='v'
nodes_by_vsup_id[last_id] << n
end
puts nodes_by_vsup_id['21']
#=> <sup class="v" id="21">
#=> 2
#=> </sup>
#=>
#=> now this is a different section
#=> <p></p>
#=>
#=> how do we keep this separate
#=> <sup class="footnote" value="fn2">
#=> [x]
#=> </sup>
或者,如果你真的不想将分隔'sup'作为集合的一部分,而是:
doc.at('body').elements.each do |n|
if n['class']=='v'
last_id = n['id']
else
nodes_by_vsup_id[last_id] << n
end
end
这是一种替代的,甚至更通用的解决方案:
class Nokogiri::XML::NodeSet
# Yields each node in the set to your block
# Returns a hash keyed by whatever your block returns
# Any nodes that return nil/false are grouped with the previous valid value
def group_chunks
Hash.new{ |k,v| k[v] = self.class.new(document) }.tap do |result|
key = nil
each{ |n| result[key = yield(n) || key] << n }
end
end
end
root_items = doc.at('body').children
separated = root_items.group_chunks{ |node| node['class']=='v' && node['id'] }
puts separated['21']
答案 2 :(得分:-1)
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("http://www.yoururl"))
doc.xpath('//sup[id="21"]').each do |node|
puts node.text
end