InQusitive InQusitive - 1 month ago 7
Ruby Question

How to get nokogiri attribute value?

My xml contains multiple statements like

<House name="bla"><Room id="bla" name="black" ><blah id="blue" name="brown"></blah></Room></House>


I need to get all the values for the given keyword.

I used
nodes = doc.css("[name]")
to get the
<Room id="bla" name="black" ><blah id="blue" name="brown"></blah></Room>
.\

But how do I get the value for a key from this. Is there any easier way to do this?

Answer
node_names = doc.css("[name]").map { |node| node['name'] }

for all node names; or for just "black",

black = doc.at_css("[name]")['name']