Reputation: 195
can someone help me in extracting the node value for the element "Name".
Type 1: I am able to extract the "name" value for the below xml by using the below code
<Element>
<Details>
<ID>20367</ID>
<Name>Ram</Name>
<Name>Sam</Name>
</Details>
</Element>
doc = Nokogiri::XML(response.body)
values = doc.xpath('//Name').map{ |node| node.text}.join ','
puts values
Output: Ram,Sam
Type 2: Now, I am need to get the same formatted output for the below xml, How can I get it
<Response xmlns="http://abc.def" xmlns:i="http://www.org">
<Name>Ram</Name>
<Name>Sam</Name>
</Response>
When I use the same code for this, I am not getting any output or error.
Depends on the user's input, I get either the type 1 xml or type 2 xml as ouput. For the both the case I need to extract the "name" element value. If it is more than one element with the same name then comma separate the values.
Upvotes: 2
Views: 2430
Reputation: 474
Try to use css
instead of xpath
, this will work for you,
doc = Nokogiri::XML(response.body)
values = doc.css('Name').select{|name| name.text}.join','
puts values
=> Ram,Sam
Upvotes: 2