Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nokogiri : List attributes name and value?

I would like to parse my XML file. I need to count attributes and have their name and value...

require 'nokogiri'
f = File.open("file.xml")
doc = Nokogiri::XML(f)
f.close
p doc.xpath("/a/@*").count

I would like to have the list of attributes.

I tried :

doc.attributes.each do |name,attr|
  p name => attr.value
end

But error :

in `<main>': undefined method `attributes' for #<Nokogiri::XML::Document:0x3490680> (NoMethodError)

I don't know why ...

<a ata="1" atb="2" atc="3">Blabla</a>

Or I would like to be able to have the name. Like :

puts doc.xpath("/a/@*").attributes[1].name
puts doc.xpath("/a/@*").attributes[1].value

To have the name and value of the first attribute. But of course, these lines don't work !

Thank you.

like image 302
Alexis_user Avatar asked May 31 '14 20:05

Alexis_user


1 Answers

Here I tried to tell you, why you got the error and how to solve your problem.

require 'nokogiri'

doc = Nokogiri::HTML <<-html
<a ata="1" atb="2" atc="3">Blabla</a>
html

doc.class  # => Nokogiri::HTML::Document
doc.respond_to?(:attributes) # => false
doc.at_css('a').class # => Nokogiri::XML::Element
doc.at_css('a').respond_to?(:attributes) # => true

Hash[doc.at_css('a').to_a]
# => {"ata"=>"1", "atb"=>"2", "atc"=>"3"}

Now reason of error undefined method 'attributes' is - your doc gives a Nokogiri::XML::Document object, which doesn't respond to attributes method. attributes is a method of the Nokogiri::XML::Node class instances.

Now, there is a multiple way to do this. #at_css will give us Nokogiri::XML::Node, where as css will give you Nokogiri::XML::NodeSet, which is a collection of Nokogiri::XML::Node.

Thus, while you want to collect all the elements of as attributes. Then you probably do as below :

doc.css('a').each do |node|
   node.each do |attr_name,attr_val|
     # work with attr_name,attr_val
   end
end

Example :-

doc.at_css('a').each { |att_name, att_value| p [att_name, att_value] }
# >> ["ata", "1"]
# >> ["atb", "2"]
# >> ["atc", "3"]

You can also use the method #attributes. Just here is an example to show you how it works actually -

node_attributes = doc.at('a').attributes
# => {"ata"=>#(Attr:0x4260fc6 { name = "ata", value = "1" }),
#     "atb"=>#(Attr:0x4260fbc { name = "atb", value = "2" }),
#     "atc"=>#(Attr:0x4260fb2 { name = "atc", value = "3" })}

node_attributes.do |atr_name,node_attr| 
  node_attributes[atr_name] = node_attr.content
end

node_attributes  # => {"ata"=>"1", "atb"=>"2", "atc"=>"3"}
like image 123
Arup Rakshit Avatar answered Nov 17 '22 15:11

Arup Rakshit