InQusitive InQusitive - 1 month ago 7
Ruby Question

Why isn't my Nokogiri working?

This is my first code in ruby using Nokogiri. But it doesn't work and I am not able to understand why.

<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>


code:

require 'nokogiri'

f = File.open("test.xml")
doc = Nokogiri::XML(f)
nodes = doc.xpath("title")
nodes.each do |node|
print node.text
end


Here I am getting the nodes as a nil class.

Answer

To select nodes in the document from the current node that match the selection no matter where they are try

nodes = doc.xpath("//title")
Comments