Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created November 26, 2008 21:32
Show Gist options
  • Save tenderlove/29584 to your computer and use it in GitHub Desktop.
Save tenderlove/29584 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::XML(open('http://search.twitter.com/search.atom?q=%22Black+Friday%22'))
doc.xpath('//ns:title', { 'ns' => 'http://www.w3.org/2005/Atom' }).each do |title|
puts title
end
__END__
####
# Why namespaces are important
#
# Take this XML for example:
<root>
<car xmlns:part="http://general-motors.com/">
<part:tire>Michelin Model XGV</part:tire>
</car>
<bicycle xmlns:part="http://schwinn.com/">
<part:tire>I'm a bicycle tire!</part:tire>
</bicycle>
</root>
###
# Say you were to search that document for a 'part:tire'. You would
# get back 2 nodes. Unfortunately you were not using proper namespaces,
# which means you cannot tell the difference between a bicycle tire and
# a car tire.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment