Created
November 26, 2008 21:32
-
-
Save tenderlove/29584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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