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