1# rexml_xpath_attribute_query.rb 2# May 16, 2007 3# 4 5require 'test/unit' 6require 'rexml/document' 7 8class TestRexmlXpathAttributeQuery < Test::Unit::TestCase 9 10 # xmlstr1 and xmlstr2 only differ in the second line - namespaces in the root element 11 @@xmlstr1 = '<?xml version="1.0" encoding="UTF-8"?> 12<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gCal="http://schemas.google.com/gCal/2005"> 13 <id>http://www.google.com/calendar/feeds/me%40gmail.com</id> 14 <entry> 15 <id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id> 16 <published>2007-05-16T13:42:27.942Z</published> 17 <updated>2007-05-15T03:29:28.000Z</updated> 18 <title type="text">My Calendar</title> 19 <link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/> 20 <link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/> 21 <link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/> 22 <author> 23 <name>Me</name> 24 <email>me@gmail.com</email> 25 </author> 26 </entry> 27</feed>' 28 29 30 @@xmlstr2 = '<?xml version="1.0" encoding="UTF-8"?> 31<feed> 32 <id>http://www.google.com/calendar/feeds/me%40gmail.com</id> 33 <entry> 34 <id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id> 35 <published>2007-05-16T13:42:27.942Z</published> 36 <updated>2007-05-15T03:29:28.000Z</updated> 37 <title type="text">My Calendar</title> 38 <link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/> 39 <link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/> 40 <link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/> 41 <author> 42 <name>Me</name> 43 <email>me@gmail.com</email> 44 </author> 45 </entry> 46</feed>' 47 48 # Fails 49 def test_xpath_query 50 do_test @@xmlstr1 51 end 52 53 # Passes 54 def test_xpath_query_no_namespace 55 do_test @@xmlstr2 56 end 57 58 def do_test(xmlString) 59 hrefs = [ 60 "http://www.google.com/calendar/feeds/me%40gmail.com/private/full", 61 "http://www.google.com/calendar/feeds/me%40gmail.com/acl/full", 62 "http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com" 63 ] 64 ctr=0 65 REXML::Document.new(xmlString).elements.each("feed/entry") do |element| 66 @alternate_link = element.elements["link[@rel='alternate']"] 67 assert_not_nil( @alternate_link ) 68 assert_equal( hrefs[ctr], @alternate_link.attributes['href']) 69 ctr += 1 70 end 71 end 72 73 74 def test_another_way 75 doc = REXML::Document.new(@@xmlstr1) 76 hrefs = [ 77 "http://www.google.com/calendar/feeds/me%40gmail.com/private/full", 78 "http://www.google.com/calendar/feeds/me%40gmail.com/acl/full", 79 "http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com" 80 ] 81 ctr=0 82 REXML::XPath.each(doc, "//link[@rel='alternate']") do |element| 83 @alternate_link = element 84 assert_not_nil @alternate_link 85 assert_equal( hrefs[ctr], @alternate_link.attributes['href']) 86 ctr += 1 87 end 88 end 89end 90