cbmeeks
cbmeeks

Reputation: 11420

How do I grab this value from Nokogiri?

Say I have:

<div class="amt" id="displayFare-1_69-61-0" style="">
  <div class="per">per person</div>
  <div class="per" id="showTotalSubIndex-1_69-61-0" style="">Total $334</div>
    $293
</div>

I want to grab just the $334. It will always have "Total $" but the id showTotalSubIndex... will be dynamic so I can't use that.

Upvotes: 0

Views: 1744

Answers (3)

the Tin Man
the Tin Man

Reputation: 160551

Both of these work:

require 'nokogiri'
doc = Nokogiri::XML(xml)
doc.search('//div[@id]/text()').select{ |n| n.text['Total'] }.first.text.split.last

and

doc.search('//div/text()').select{ |n| n.text['Total'] }.first.text.split.last

The difference is the first should run a bit faster if you know the div you're looking for always has an id.

If the ID always starts with "showTotalSubIndex" you could use:

doc.at('//div[starts-with(@id,"showTotalSubIndex")]').first.text.split.last

and if you know there's only going to be one in the document, you can use:

doc.at('//div[starts-with(@id,"showTotalSubIndex")]').text.split.last

EDIT:

Ryan posits the idea the XML structure might be consistent. If so:

doc.at('//div[2]').text[/(\$\d+)/, 1]

:-)

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

Rather than finding the text:

html = Nokogiri::HTML(html)
html.css("div.amt").children[1].text.gsub(/^Total /, '')

I assume here that the HTML is structured in such a way that the second child of any div.amt tag is the value that you're after, and then we'll just grab the text of that and gsub it.

Upvotes: 0

Nikolaus Gradwohl
Nikolaus Gradwohl

Reputation: 20124

You can use a nokogiri xpath expression to iterate over all the div nodes and scan the string for the 'Total $' Prefix like this

require 'rubygems'
require 'nokogiri'

doc = Nokogiri::XML.parse( open( "test.xml" ))
doc.xpath("//div/text()").each{ |t|
   tmp = t.to_str.strip
   puts tmp[7..-1] if tmp.index('Total $') == 0
}

Upvotes: 1

Related Questions