开发者

getting value using xpath, ruby

开发者 https://www.devze.com 2023-02-12 23:35 出处:网络
I need to get value 9,70 from the following code, but am unable to do so. The number\'s comma is part of number and not delimiter, so the whole number is needed in one string. id=\"cheapest wine\" is

I need to get value 9,70 from the following code, but am unable to do so. The number's comma is part of number and not delimiter, so the whole number is needed in one string. id="cheapest wine" is unique, but it keeps returning error.

<tr class="chartTableHeader">
<tr class="chartTableRow">
  <td class="chartTableColFirst" style="height: 19px">
  <td class="chartTableCol" style="height: 19px">
  <td class="chartTableCol" style="height: 19px">
  <span id="cheapest wine">9,70</span>
  </td>
  <td class="chartTableCol" style="height: 19px">
  <td cl开发者_如何转开发ass="chartTableCol" style="height: 19px">
  <td class="chartTableCol" style="height: 19px">


Using Nokogiri, and assuming that your html is formatted properly, you can get the value as follows:

require 'nokogiri'

xml = <<-EOF
<root>
<span id="cheapest wine">9,70</span>
</root>
EOF

doc = Nokogiri::XML(xml)
doc.xpath('//span[@id="cheapest wine"]').map do |add|
  puts add.inner_text
end

Here the key is the XPath query: //span[@id="cheapest wine"] which searches for the span nodes whose id is "cheapest wine" (being an id, there should only be one).


Use the following XPath expression:

number(
    translate(tr[@class='chartTableRow']/td/span[@id='cheapest wine'], 
             ',', 
             '.'
             )
      )

where the current node from which the XPath expression is evaluated is the parent of the XML fragment shown in your question.

The XPath expression above evaluates to 9.7

0

精彩评论

暂无评论...
验证码 换一张
取 消