Nokogiri : List attributes name and value?
Asked Answered
I

1

8

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.

Immediacy answered 31/5, 2014 at 20:46 Comment(0)
M
5

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"}
Maribeth answered 31/5, 2014 at 21:1 Comment(3)
Thank you for your answere and the speed ! I'm used to use xpath (ahah I have learned nokogiri for 1day) but it works very well !! Now I can get name and value of attributes with "doc.at_xpath("/a").to_a[0][0]". I saw into documentation what is "Nokogiri::XML::NodeSet" but I didn't know how to get a Nokogiri::XML::Node (I thought that it was with xpath/css ...)Immediacy
Just to go further, why "puts doc.at_css('a').class" is Nokogiri::XML::Element and not Nokogiri::XML::Node ? (as you have said)Immediacy
@user3604482 That in one sentence is OOP. But here is the answer for you a bit more expressive - doc.at('a').class.superclass # => Nokogiri::XML::Node. Thus Element is Node too...Maribeth

© 2022 - 2024 — McMap. All rights reserved.