Get parent node from node to xml-nokogiri-rails

Given the XML data:

  <questionpaper>
   <question1>
      <id>540<id>
      <content>....</content>
   </question1>
   <question2>
      <id>550<id>
      <content>....</content>
   </question2>
   .
   .
   .
   <question10>
      <id>560<id>
      <content>....</content>
   </question10>

      

If I give 540 i, e id I have to get its parent node, which is question1. I am using nokogiri and have tried

   @qpid = Qpaper.find(params[:id])
   file = File.open(@qpid.qpaper_file)
   xml = Nokogiri::XML(file)
   quest = xml.search("//id[text()= '#{params[:qno]}']")     //params[:qno] gives id
   render text: quest.parent

      

It gives error

  undefined method `parent' for #<Nokogiri::XML::NodeSet:0xe603890>

      

Please help me to solve it. Thank.

+3


source to share


1 answer


quest

contains Nokogiri::XML::NodeSet

, not one Node

. Call the parent on one of the nodes in node instead:



quest = xml.search("//id[text()= '#{params[:qno]}']")
render text: quest.first.parent

      

+2


source







All Articles