Search code examples
rubynokogirinodes

How to get the content of an XML node using XPath and Nokogiri


I have code like this:

@doc = Nokogiri::HTML(open(url)
@doc.xpath(query).each do |html|

  puts html # how get content of a node
end

How do I get the content of the node instead of something like this:

<li class="stat">

Solution

  • This is the Synopsis example in the README file for Nokogiri showing one way to do it using CSS, XPath or a hybrid:

    require 'nokogiri'
    require 'open-uri'
    
    # Get a Nokogiri::HTML:Document for the page we’re interested in...
    
    doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
    
    # Do funky things with it using Nokogiri::XML::Node methods...
    
    ####
    # Search for nodes by css
    doc.css('h3.r a.l').each do |link|
      puts link.content
    end
    
    ####
    # Search for nodes by xpath
    doc.xpath('//h3/a[@class="l"]').each do |link|
      puts link.content
    end
    
    ####
    # Or mix and match.
    doc.search('h3.r a.l', '//h3/a[@class="l"]').each do |link|
      puts link.content
    end