How to get XML parent attribute value

三世轮回 提交于 2019-12-12 05:16:37

问题


I have multiple statements like:

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

When the blah name is some particular value like test7 I need the corresponding Room name. How do I achieve that?


回答1:


I never used Nokogiri but I tried and this seems to work:

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

Just check for nils.

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


来源:https://stackoverflow.com/questions/40103791/how-to-get-xml-parent-attribute-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!