Reputation: 15374
Im trying to figure out how to parse returned XMl via an api call, Im using Nokogiri and am trying to use xpath. What i would like to do is display all "title" attributes that have been returned
So far i have in a method
def getcontact
doc = Nokogiri::XML(open(url))
doc.xpath('//xmlns:feed/xmlns:entry')
end
controller
@mycontacts = getcontact
View
<% @mycontacts.each do |c| %>
<%= c.xpath("//title") %>
<% end %>
The XML
<entry>
<id>xxx</id>
<updated>xxx</updated>
<category scheme="xxx" term="xxx"/>
<title type="text">xxx</title>
<link rel="xxx" type="xxx" href="xxx"/>
<link rel="xxx0gmail.com/b6ea0e8ddbc4e5"/>
<link rel="xx" type="xxx" href="xxx"/> <link rel="xx" type="axx" href="xxx"/>
<gd:email rel="xxx" address="xxx" primary="xx"/>
</entry>
i am getting nothing returned, could someone point out what i am doing wrong please, also i notice there is html tags in the returned XML, can i strip this out, for example type=text is within the title attribute
Update
So i have tried this
doc.xpath('//xmlns:feed/xmlns:entry/xmlns:title').text
but this returns all the titles as a string
Update 2
View
<% @mycontacts.each do |c| %>
<%= c.xpath('xmlns:title').text %><br>
<% end %>
method
doc.xpath('//xmlns:feed/xmlns:entry')
so this lists all my titles but if there are any blank entries there is an empty record. Need to remove these from the loop now i guess
Is this correct, is there a better way to do it?
Thanks
Upvotes: 0
Views: 2585
Reputation: 6419
With method 2, try using:
d.xpath('//feed/entry[title[node()]]'
This will return a nodeset containing nodes that have a non-empty title. Then you can iterate over set however you like.
Upvotes: 2