A simple XPATh failing

Name: PeterPan
Tag: Critical
Priority: 223

I am doing below and I get Name:/Tag:/Priority but also I need details
i.e.
Name: PeterPan
Tag: Critical and so on

doc.xpath("//p/span").each do |para_tag|
puts para_tag.text
end

Any input will be helpful

On Sun, Jan 22, 2012 at 12:32 PM, Ruby M. [email protected]
wrote:

Name: PeterPan

I am doing below and I get Name:/Tag:/Priority but also I need details
i.e.
Name: PeterPan

doc.xpath(“//p/span”).each do |para_tag|

  • puts para_tag.text
  • puts “#{para_tag.text} #{para_tag.next_sibling.text}”

end

HTH,

Hi Hassan,

Thanks a bunch :smiley: completely forgot the next sibling option and was
trying following-siblings :slight_smile:

Hassan S. wrote in post #1042072:

On Sun, Jan 22, 2012 at 12:32 PM, Ruby M. [email protected]
wrote:

Name: PeterPan

I am doing below and I get Name:/Tag:/Priority but also I need details
i.e.
Name: PeterPan

doc.xpath(“//p/span”).each do |para_tag|

  • puts para_tag.text
  • puts “#{para_tag.text} #{para_tag.next_sibling.text}”

end

HTH,

On Sun, Jan 22, 2012 at 10:25 PM, Ruby M. [email protected]
wrote:

Thanks a bunch :smiley: completely forgot the next sibling option and was
trying following-siblings :slight_smile:

If information should be grouped by

, I’d do a two step approach,
something like this

require ‘nokogiri’

dom = Nokogiri.HTML(<<DOC)

Name: PeterPan
Tag: Critical
Priority: 223

DOC

dom.xpath(‘//p’).each do |para_tag|
dat = {}

para_tag.xpath(‘span[@class=“label”]’).each do |span_tag|
dat[span_tag.text[/\A(.*?):?\z/, 1]] = span_tag.next_sibling.text
end

p dat
end

Kind regards

robert