libxml4r 0.0.0 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +30 -0
- data/README.rdoc +113 -9
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/lib/libxml4r.rb +229 -0
- data/libxml4r.gemspec +98 -0
- data/test/test_helper.rb +39 -0
- data/test/test_libxml4r.rb +34 -3
- data/test/test_xml_modifiers.rb +57 -0
- data/test/test_xml_outputters.rb +53 -0
- data/test/test_xml_readers.rb +59 -0
- data/test/xml/atom.xml +13 -0
- data/test/xml/books.xml +146 -0
- data/test/xml/breakfast_menu.xml +38 -0
- data/test/xml/document.xml +49 -0
- data/test/xml/merge_bug_data.xml +58 -0
- data/test/xml/namespaces.xml +22 -0
- data/test/xml/parts_system.xml +31 -0
- data/test/xml/plant_catalog.xml +336 -0
- data/test/xml/recipie.xml +18 -0
- data/test/xml/rss1.xml +58 -0
- data/test/xml/rss2.xml +56 -0
- data/test/xml/ruby_lang.xhtml +238 -0
- data/test/xml/rubynet.xml +79 -0
- data/test/xml/shiporder.xls +86 -0
- data/test/xml/shiporder.xml +23 -0
- data/test/xml/shiporder.xsd +31 -0
- data/test/xml/soap_create_path.xml +15 -0
- data/test/xml/soap_create_path_response.xml +16 -0
- data/test/xml/soap_create_reservation.xml +25 -0
- data/test/xml/soap_create_reservation_response.xml +64 -0
- data/test/xml/soap_get_price.xml +11 -0
- data/test/xml/soap_get_price_response.xml +11 -0
- data/test/xml/soap_manufacturer_names_response.xml +27 -0
- data/test/xml/soap_order_item.xml +17 -0
- data/test/xml/soap_order_item_response.xml +10 -0
- data/test/xml/soap_refresh_path.xml +15 -0
- data/test/xml/soap_refresh_path_response.xml +16 -0
- data/test/xml/soap_teardown_path.xml +15 -0
- data/test/xml/soap_teardown_path_response.xml +16 -0
- metadata +52 -3
data/test/test_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
# require 'woulda'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
require 'libxml4r'
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
|
12
|
+
|
13
|
+
def self.obj(object, &block)
|
14
|
+
context "#{object.class} #{object.to_s}" do
|
15
|
+
setup do
|
16
|
+
@obj = object
|
17
|
+
end
|
18
|
+
|
19
|
+
context '' do
|
20
|
+
yield
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.should_include(*m)
|
26
|
+
a = [m] if m.class == Symbol
|
27
|
+
a = m if m.class == Array && m.first.class == Symbol
|
28
|
+
raise "Requires a symbol, or array of symbols" if a.nil?
|
29
|
+
a.each do |sym|
|
30
|
+
should "include #{sym}" do
|
31
|
+
# puts @obj
|
32
|
+
# puts @obj.class
|
33
|
+
assert @obj.include? sym.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
data/test/test_libxml4r.rb
CHANGED
@@ -1,7 +1,38 @@
|
|
1
|
-
require '
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestLibxml4r < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
|
5
|
+
should "check that libxml4r classes are loaded" do
|
6
|
+
assert Libxml4r::XmlDocument
|
7
|
+
assert Libxml4r::XmlNode
|
8
|
+
assert Libxml4r::String
|
6
9
|
end
|
10
|
+
|
11
|
+
should "check that modules are included" do
|
12
|
+
assert LibXML::XML::Document.included_modules.include? Libxml4r::XML::Document
|
13
|
+
assert LibXML::XML::Node.included_modules.include? Libxml4r::XML::Node
|
14
|
+
assert String.included_modules.include? Libxml4r::String
|
15
|
+
end
|
16
|
+
|
17
|
+
obj Libxml4r::XmlDocument::instance_methods do
|
18
|
+
should_include :node, :nodes # xml readers
|
19
|
+
should_include :strip!, :last= # xml modifiers
|
20
|
+
should_include :to_xml # xml outputters
|
21
|
+
end
|
22
|
+
|
23
|
+
obj Libxml4r::XmlNode::instance_methods do
|
24
|
+
should_include :node, :nodes # xml readers
|
25
|
+
should_include :strip!, :last=, :replace! # xml modifiers
|
26
|
+
should_include :to_xml, :inner_html # xml outputters
|
27
|
+
end
|
28
|
+
|
29
|
+
obj Libxml4r::XmlNode::SearchNodes::instance_methods do
|
30
|
+
should_include :[] # xml readers
|
31
|
+
end
|
32
|
+
|
33
|
+
obj Libxml4r::String::instance_methods do
|
34
|
+
should_include :to_xmldoc
|
35
|
+
end
|
36
|
+
|
7
37
|
end
|
38
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestXmlModifiers < Test::Unit::TestCase
|
4
|
+
context "A namespaced document" do
|
5
|
+
setup do
|
6
|
+
# choose xml file
|
7
|
+
# xml_file = "namespaces.xml"
|
8
|
+
# xml_file = "rss1.xml"
|
9
|
+
# xml_file = "rubynet.xml"
|
10
|
+
# xml_file = "shiporder.xml"
|
11
|
+
xml_file = "soap_manufacturer_names_response.xml"
|
12
|
+
# xml_file = "soap_create_reservation_response.xml"
|
13
|
+
|
14
|
+
# Load xml file
|
15
|
+
xml_file = File.join(File.dirname(__FILE__), "xml", xml_file)
|
16
|
+
assert ! xml_file.nil?
|
17
|
+
|
18
|
+
# doc_str = File.open( xml_file, "rb").read
|
19
|
+
# @doc = doc_str.to_xmldoc
|
20
|
+
@doc = XML::Document.file xml_file
|
21
|
+
assert ! @doc.nil?
|
22
|
+
|
23
|
+
# Remove namespaces
|
24
|
+
@doc.strip!
|
25
|
+
end
|
26
|
+
|
27
|
+
should "strip all namespaces from a document" do
|
28
|
+
assert ! @doc.strip!.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "a partially namespaced document" do
|
33
|
+
setup do
|
34
|
+
# Insert the root node of a namespaced document into a stripped document
|
35
|
+
end
|
36
|
+
|
37
|
+
# should "replace a node within a document" do
|
38
|
+
# assert ! @doc.strip!.nil?
|
39
|
+
# end
|
40
|
+
|
41
|
+
# should "replace a node which is the root node of the document" do
|
42
|
+
# assert ! @doc.strip!.nil?
|
43
|
+
# end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "a stripped document" do
|
47
|
+
|
48
|
+
should "strip all namespaces from a node" do
|
49
|
+
@doc.node["/"]
|
50
|
+
assert ! @node.strip!.nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestXmlOutputters < Test::Unit::TestCase
|
4
|
+
context "A namespaced xml document" do
|
5
|
+
setup do
|
6
|
+
# xml_file = "rss1.xml"
|
7
|
+
xml_file = "ruby_lang.xhtml"
|
8
|
+
|
9
|
+
# Load xml file
|
10
|
+
xml_file = File.join(File.dirname(__FILE__), "xml", xml_file)
|
11
|
+
assert ! xml_file.nil?
|
12
|
+
|
13
|
+
@doc = XML::Document.file xml_file
|
14
|
+
assert ! @doc.nil?
|
15
|
+
|
16
|
+
@doc.strip! # Remove any namespaces
|
17
|
+
|
18
|
+
@paths=[
|
19
|
+
"/html/body/div[@id='page']"
|
20
|
+
"/html/body//div[@id='intro']"
|
21
|
+
]
|
22
|
+
|
23
|
+
@replacement_node = XML::Node.new("NameString","ContentString")
|
24
|
+
end
|
25
|
+
|
26
|
+
should "return the inner html of a valid xhtml node" do
|
27
|
+
@paths.each do |path|
|
28
|
+
inner_html = @doc.node[path].inner_html
|
29
|
+
inner_xml = @doc.node[path].inner_xml
|
30
|
+
|
31
|
+
assert ! inner_html.nil?
|
32
|
+
assert inner_html == inner_xml
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
should "return an xml string of the node" do
|
37
|
+
@paths.each do |path|
|
38
|
+
xml_string = @doc.node[path].to_xml
|
39
|
+
s = @doc.node[path].to_s
|
40
|
+
assert ! xml_string.nil?
|
41
|
+
assert xml_string == s
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
should "return an xml string of the document" do
|
46
|
+
xml_string = @doc.to_xml
|
47
|
+
s = @doc.to_s
|
48
|
+
assert ! xml_string.nil?
|
49
|
+
assert xml_string == s
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestXmlReaders < Test::Unit::TestCase
|
4
|
+
context "A namespaced xml document" do
|
5
|
+
setup do
|
6
|
+
# choose xml file
|
7
|
+
# xml_file = "namespaces.xml"
|
8
|
+
# xml_file = "rss1.xml"
|
9
|
+
# xml_file = "rubynet.xml"
|
10
|
+
# xml_file = "shiporder.xml"
|
11
|
+
xml_file = "soap_manufacturer_names_response.xml"
|
12
|
+
# xml_file = "soap_create_reservation_response.xml"
|
13
|
+
|
14
|
+
# Load xml file
|
15
|
+
xml_file = File.join(File.dirname(__FILE__), "xml", xml_file)
|
16
|
+
assert ! xml_file.nil?
|
17
|
+
|
18
|
+
# doc_str = File.open( xml_file, "rb").read
|
19
|
+
# @doc = doc_str.to_xmldoc
|
20
|
+
@doc = XML::Document.file xml_file
|
21
|
+
assert ! @doc.nil?
|
22
|
+
|
23
|
+
@doc.strip! # Remove any namespaces
|
24
|
+
|
25
|
+
# path="/Envelope/Body"
|
26
|
+
# path="getManufacturerNamesResponse"
|
27
|
+
# path="IDAndNameList"
|
28
|
+
# path="IDAndName"
|
29
|
+
@paths=[
|
30
|
+
"/Envelope/Body/getManufacturerNamesResponse/IDAndNameList",
|
31
|
+
"/Envelope/Body/getManufacturerNamesResponse/IDAndNameList/IdAndName",
|
32
|
+
"/Envelope/Body/getManufacturerNamesResponse/IDAndNameList/IdAndName/name",
|
33
|
+
"/Envelope/Body/getManufacturerNamesResponse/IDAndNameList/IdAndName/name/id",
|
34
|
+
]
|
35
|
+
|
36
|
+
@replacement_node = XML::Node.new("NameString","ContentString")
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with any valid syntax xpath expression" do
|
40
|
+
should "return the first matching node" do
|
41
|
+
@paths.each do |path|
|
42
|
+
# node = @doc.node[path]
|
43
|
+
# puts "node=#{node}"
|
44
|
+
assert ! node.nil?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
should "return an array of matching nodes" do
|
48
|
+
@paths.each do |path|
|
49
|
+
# nodes = @doc.nodes[path]
|
50
|
+
# puts "nodes=#{nodes}"
|
51
|
+
assert ! node.nil?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
data/test/xml/atom.xml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
2
|
+
<?xml-stylesheet type="text/xsl" href="my_stylesheet.xsl"?>
|
3
|
+
<feed xmlns="http://www.w3.org/2005/Atom">
|
4
|
+
<!-- Not a valid atom entry -->
|
5
|
+
<entry>
|
6
|
+
<title type="html"><![CDATA[<<strong>>]]></title>
|
7
|
+
<content type="xhtml">
|
8
|
+
<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
9
|
+
<xhtml:p>hi there</xhtml:p>
|
10
|
+
</xhtml:div>
|
11
|
+
</content>
|
12
|
+
</entry>
|
13
|
+
</feed>
|
data/test/xml/books.xml
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
<?xml version="1.0"?>
|
2
|
+
<!-- Sample xml file from Microsoft.
|
3
|
+
See http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx -->
|
4
|
+
<catalog>
|
5
|
+
<book id="bk101">
|
6
|
+
<author>Gambardella, Matthew</author>
|
7
|
+
<title>XML Developer's Guide</title>
|
8
|
+
<genre>Computer</genre>
|
9
|
+
<price>44.95</price>
|
10
|
+
<publish_date>2000-10-01</publish_date>
|
11
|
+
<description>
|
12
|
+
An in-depth look at creating applications
|
13
|
+
with XML.
|
14
|
+
</description>
|
15
|
+
</book>
|
16
|
+
<book id="bk102">
|
17
|
+
<author>Ralls, Kim</author>
|
18
|
+
<title>Midnight Rain</title>
|
19
|
+
<genre>Fantasy</genre>
|
20
|
+
<price>5.95</price>
|
21
|
+
<publish_date>2000-12-16</publish_date>
|
22
|
+
<description>
|
23
|
+
A former architect battles corporate zombies,
|
24
|
+
an evil sorceress, and her own childhood to become queen
|
25
|
+
of the world.
|
26
|
+
</description>
|
27
|
+
</book>
|
28
|
+
<book id="bk103">
|
29
|
+
<author>Corets, Eva</author>
|
30
|
+
<title>Maeve Ascendant</title>
|
31
|
+
<genre>Fantasy</genre>
|
32
|
+
<price>5.95</price>
|
33
|
+
<publish_date>2000-11-17</publish_date>
|
34
|
+
<description>
|
35
|
+
After the collapse of a nanotechnology
|
36
|
+
society in England, the young survivors lay the
|
37
|
+
foundation for a new society.
|
38
|
+
</description>
|
39
|
+
</book>
|
40
|
+
<book id="bk104">
|
41
|
+
<author>Corets, Eva</author>
|
42
|
+
<title>Oberon's Legacy</title>
|
43
|
+
<genre>Fantasy</genre>
|
44
|
+
<price>5.95</price>
|
45
|
+
<publish_date>2001-03-10</publish_date>
|
46
|
+
<description>
|
47
|
+
In post-apocalypse England, the mysterious
|
48
|
+
agent known only as Oberon helps to create a new life
|
49
|
+
for the inhabitants of London. Sequel to Maeve
|
50
|
+
Ascendant.
|
51
|
+
</description>
|
52
|
+
</book>
|
53
|
+
<book id="bk105">
|
54
|
+
<author>Corets, Eva</author>
|
55
|
+
<title>The Sundered Grail</title>
|
56
|
+
<genre>Fantasy</genre>
|
57
|
+
<price>5.95</price>
|
58
|
+
<publish_date>2001-09-10</publish_date>
|
59
|
+
<description>
|
60
|
+
The two daughters of Maeve, half-sisters,
|
61
|
+
battle one another for control of England. Sequel to
|
62
|
+
Oberon's Legacy.
|
63
|
+
</description>
|
64
|
+
</book>
|
65
|
+
<book id="bk106">
|
66
|
+
<author>Randall, Cynthia</author>
|
67
|
+
<title>Lover Birds</title>
|
68
|
+
<genre>Romance</genre>
|
69
|
+
<price>4.95</price>
|
70
|
+
<publish_date>2000-09-02</publish_date>
|
71
|
+
<description>
|
72
|
+
When Carla meets Paul at an ornithology
|
73
|
+
conference, tempers fly as feathers get ruffled.
|
74
|
+
</description>
|
75
|
+
</book>
|
76
|
+
<book id="bk107">
|
77
|
+
<author>Thurman, Paula</author>
|
78
|
+
<title>Splish Splash</title>
|
79
|
+
<genre>Romance</genre>
|
80
|
+
<price>4.95</price>
|
81
|
+
<publish_date>2000-11-02</publish_date>
|
82
|
+
<description>
|
83
|
+
A deep sea diver finds true love twenty
|
84
|
+
thousand leagues beneath the sea.
|
85
|
+
</description>
|
86
|
+
</book>
|
87
|
+
<book id="bk108">
|
88
|
+
<author>Knorr, Stefan</author>
|
89
|
+
<title>Creepy Crawlies</title>
|
90
|
+
<genre>Horror</genre>
|
91
|
+
<price>4.95</price>
|
92
|
+
<publish_date>2000-12-06</publish_date>
|
93
|
+
<description>
|
94
|
+
An anthology of horror stories about roaches,
|
95
|
+
centipedes, scorpions and other insects.
|
96
|
+
</description>
|
97
|
+
</book>
|
98
|
+
<book id="bk109">
|
99
|
+
<author>Kress, Peter</author>
|
100
|
+
<title>Paradox Lost</title>
|
101
|
+
<genre>Science Fiction</genre>
|
102
|
+
<price>6.95</price>
|
103
|
+
<publish_date>2000-11-02</publish_date>
|
104
|
+
<description>
|
105
|
+
After an inadvertant trip through a Heisenberg
|
106
|
+
Uncertainty Device, James Salway discovers the problems
|
107
|
+
of being quantum.
|
108
|
+
</description>
|
109
|
+
</book>
|
110
|
+
<book id="bk110">
|
111
|
+
<author>O'Brien, Tim</author>
|
112
|
+
<title>Microsoft .NET: The Programming Bible</title>
|
113
|
+
<genre>Computer</genre>
|
114
|
+
<price>36.95</price>
|
115
|
+
<publish_date>2000-12-09</publish_date>
|
116
|
+
<description>
|
117
|
+
Microsoft's .NET initiative is explored in
|
118
|
+
detail in this deep programmer's reference.
|
119
|
+
</description>
|
120
|
+
</book>
|
121
|
+
<book id="bk111">
|
122
|
+
<author>O'Brien, Tim</author>
|
123
|
+
<title>MSXML3: A Comprehensive Guide</title>
|
124
|
+
<genre>Computer</genre>
|
125
|
+
<price>36.95</price>
|
126
|
+
<publish_date>2000-12-01</publish_date>
|
127
|
+
<description>
|
128
|
+
The Microsoft MSXML3 parser is covered in
|
129
|
+
detail, with attention to XML DOM interfaces, XSLT processing,
|
130
|
+
SAX and more.
|
131
|
+
</description>
|
132
|
+
</book>
|
133
|
+
<book id="bk112">
|
134
|
+
<author>Galos, Mike</author>
|
135
|
+
<title>Visual Studio 7: A Comprehensive Guide</title>
|
136
|
+
<genre>Computer</genre>
|
137
|
+
<price>49.95</price>
|
138
|
+
<publish_date>2001-04-16</publish_date>
|
139
|
+
<description>
|
140
|
+
Microsoft Visual Studio 7 is explored in depth,
|
141
|
+
looking at how Visual Basic, Visual C++, C#, and ASP+ are
|
142
|
+
integrated into a comprehensive development
|
143
|
+
environment.
|
144
|
+
</description>
|
145
|
+
</book>
|
146
|
+
</catalog>
|
@@ -0,0 +1,38 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1"?>
|
2
|
+
<!-- http://www.w3schools.com/XML/xml_examples.asp -->
|
3
|
+
<breakfast_menu>
|
4
|
+
<food>
|
5
|
+
<name>Belgian Waffles</name>
|
6
|
+
<price>$5.95</price>
|
7
|
+
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
|
8
|
+
<calories>650</calories>
|
9
|
+
|
10
|
+
</food>
|
11
|
+
<food>
|
12
|
+
<name>Strawberry Belgian Waffles</name>
|
13
|
+
<price>$7.95</price>
|
14
|
+
<description>light Belgian waffles covered with strawberries and whipped cream</description>
|
15
|
+
<calories>900</calories>
|
16
|
+
</food>
|
17
|
+
|
18
|
+
<food>
|
19
|
+
<name>Berry-Berry Belgian Waffles</name>
|
20
|
+
<price>$8.95</price>
|
21
|
+
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
|
22
|
+
<calories>900</calories>
|
23
|
+
</food>
|
24
|
+
<food>
|
25
|
+
|
26
|
+
<name>French Toast</name>
|
27
|
+
<price>$4.50</price>
|
28
|
+
<description>thick slices made from our homemade sourdough bread</description>
|
29
|
+
<calories>600</calories>
|
30
|
+
</food>
|
31
|
+
<food>
|
32
|
+
<name>Homestyle Breakfast</name>
|
33
|
+
|
34
|
+
<price>$6.95</price>
|
35
|
+
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
|
36
|
+
<calories>950</calories>
|
37
|
+
</food>
|
38
|
+
</breakfast_menu>
|