如何获取XML父属性值

How to get XML parent attribute value

我有多个语句,例如:

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

blah name 是像 test7 这样的特定值时,我需要相应的房间名称。我该如何实现?

我从未使用过 Nokogiri,但我试过了,这似乎有效:

 xml_doc.css('blah[name="test7"]').first.ancestors("Room").first['name']
 => "test3" 

只需检查 nil 秒。

2.3.1 :132 > xml_doc.css('blah[name="test7"]').map { |node| node.ancestors("Room").first['name'] }
 => ["test3"]