InQusitive InQusitive - 2 years ago 119
Ruby Question

How to get nokogiri parent attribute value

I have multiple statements like

<House name="test1">
<Room id="test2" name="test3" >
<test name="test4" param="test5">
<blah id="test6" name="test7">

What I want to do is when the
blah name
is some particular value(eg:
) I need the corresponding Room name in nokogiri. How do I achieve that?

Answer Source

Never used nokogiri but I tried and it seems to work

 => "test3" 

Just, check for nils ;)


2.3.1 :132 > xml_doc.css('blah[name="test7"]').map { |node| node.ancestors("Room").first['name'] }
 => ["test3"] 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download