dreamcat4-libxml-bindings 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +58 -20
- data/Rakefile +8 -3
- data/VERSION.yml +2 -2
- data/lib/libxml_bindings.rb +212 -71
- data/test/test_helper.rb +29 -0
- data/test/test_libxml_bindings.rb +38 -0
- 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 +53 -9
- data/test/libxml_bindings_test.rb +0 -7
data/README.rdoc
CHANGED
@@ -1,39 +1,59 @@
|
|
1
1
|
= libxml-bindings
|
2
2
|
|
3
|
-
|
4
|
-
credit to Phil Bogle.
|
3
|
+
{libxml-bindings}[http://github.com/dreamcat4/libxml-bindings/tree/master] is a light set of methods and bolt-ons which aren't maintained by the core {libxml ruby library}[http://libxml.rubyforge.org/install.xml]. These methods aim to provide a more convenient API interface which is provided and documented separately, but actually mixed in to extend the original LibXML::classes. Using these methods should significantly reduce the lines of code needed to perform the most common operations of accessing and manipulating an xml document structure.
|
5
4
|
|
6
|
-
|
5
|
+
For a full list of methods, please refer to the {RDoc Documentation}[http://rdoc.info/projects/dreamcat4/libxml-bindings]
|
6
|
+
and also the {Libxml-Ruby RDocs}[http://libxml.rubyforge.org/rdoc/]
|
7
7
|
|
8
|
-
|
8
|
+
For benkchmarks / performance comparison see http://cfis.savagexi.com/2008/07/16/resurrecting-libxml-ruby
|
9
|
+
|
10
|
+
== Installation
|
11
|
+
|
12
|
+
gem sources -a http://gems.github.com
|
13
|
+
gem install dreamcat4-libxml-bindings
|
14
|
+
|
15
|
+
== Getting started
|
16
|
+
|
17
|
+
You can call +to_xmldoc+ on any xml string to convert it into an XML::Document
|
9
18
|
|
10
19
|
>> s = '<foo id="1"><author>p. bogle</author><bar>content</bar><bar>cont2</bar></foo>'
|
11
|
-
>>
|
20
|
+
>> doc = s.to_xmldoc
|
12
21
|
|
13
|
-
The
|
22
|
+
The +node["/xpath"]+ method returns the first Node matching the given {xpath}[http://www.w3schools.com/XPath/xpath_syntax.asp]
|
14
23
|
|
15
|
-
>>
|
24
|
+
>> doc.node["author"]
|
16
25
|
=> <author>p. bogle</author>
|
17
26
|
|
18
|
-
The
|
27
|
+
The +nodes["/xpath"]+ method returns an array of Nodes matching the given {xpath}[http://www.w3schools.com/XPath/xpath_syntax.asp]
|
19
28
|
|
20
|
-
>>
|
29
|
+
>> doc.nodes["/foo/bar"]
|
21
30
|
=> [<bar>content</bar>, <bar>content2</bar>]
|
22
31
|
|
23
|
-
|
32
|
+
+nodes[]+ can be called with a block to iterate through each of the matching nodes
|
24
33
|
|
25
|
-
>>
|
34
|
+
>> doc.nodes["bar"] do |bar| puts bar.xpath; end
|
26
35
|
/foo/bar[1]
|
27
36
|
/foo/bar[2]
|
28
37
|
|
29
|
-
|
38
|
+
You can call +node[]+ on another node to iterate and search within a smaller context of the document
|
30
39
|
|
31
|
-
|
40
|
+
>> foo = doc.node["/foo"]
|
41
|
+
>> foo.node["author"]
|
42
|
+
=> <author>p. bogle</author>
|
43
|
+
|
44
|
+
>> foo.nodes["/bar"].each {|node| puts node.inner_xml}
|
45
|
+
=> "content2"
|
46
|
+
=> "cont2"
|
47
|
+
|
48
|
+
For more information about XPath syntax, please see http://www.w3schools.com/XPath/xpath_syntax.asp
|
32
49
|
|
33
|
-
|
50
|
+
== Namespace Stripping
|
34
51
|
|
35
|
-
|
52
|
+
The handling of default namespaces in libxml-ruby is extremely awkward and cumbersome as it requires passing along an array of namespace strings with every find() method call. It also represents ambiguity concerning the href of the default namespace.
|
36
53
|
|
54
|
+
Suppose you had a namespaced XML source with xmlns:= directives like this
|
55
|
+
|
56
|
+
>> document.to_xml
|
37
57
|
<?xml version="1.0" encoding="UTF-8"?>
|
38
58
|
<feed xmlns="http://www.w3.org/2005/Atom"
|
39
59
|
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
|
@@ -43,12 +63,30 @@ Suppose you had XML like the following
|
|
43
63
|
...
|
44
64
|
</feed>
|
45
65
|
|
46
|
-
|
66
|
+
With libxml-bindings its possible to do:
|
67
|
+
|
68
|
+
>> document.strip!
|
69
|
+
|
70
|
+
And be left with plain xml which can be parsed more easily and without the need for specifying any confusing namespace directives
|
47
71
|
|
48
|
-
|
49
|
-
|
72
|
+
>> document.to_xml
|
73
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
74
|
+
<feed>
|
75
|
+
<title type=\"text\">Phil Bogle's Contacts</title>
|
76
|
+
...
|
77
|
+
</feed>
|
78
|
+
|
79
|
+
>> document.node["/feed/title"].inner_xml
|
80
|
+
=> "Phil Bogle's Contacts"
|
81
|
+
|
82
|
+
After manipulating your data model, a default namespace can later be re-applied on the top-level node. However its generally not recommended to use more than one namespace within the same xml document.
|
50
83
|
|
51
84
|
== Copyright
|
52
85
|
|
53
|
-
Copyright (c) 2009 dreamcat4. See LICENSE for details.
|
54
|
-
|
86
|
+
Copyright (c) 2009 dreamcat4. See LICENSE for details.
|
87
|
+
|
88
|
+
== Contribution, Credits
|
89
|
+
|
90
|
+
This project was started on the top of Phil Bogle's libxml_helper.rb[http://thebogles.com/blog/an-hpricot-style-interface-to-libxml#]. Most methods have been renamed to represent the api styling of the libxml project. The coding examples above are all adapted from Phil's original explanatory texts.
|
91
|
+
|
92
|
+
Copyright (c) 2008 Phil Bogle.
|
data/Rakefile
CHANGED
@@ -5,10 +5,11 @@ begin
|
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "libxml-bindings"
|
8
|
-
gem.summary = %Q{Dreamcat4's bindings
|
8
|
+
gem.summary = %Q{Dreamcat4's bindings on libxml-ruby. Convenience methods for extending the core classes.}
|
9
9
|
gem.email = "dreamcat4@gmail.com"
|
10
10
|
gem.homepage = "http://github.com/dreamcat4/libxml-bindings"
|
11
11
|
gem.authors = ["dreamcat4"]
|
12
|
+
gem.add_dependency("libxml-ruby", ">= 1.1.3")
|
12
13
|
|
13
14
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
15
|
end
|
@@ -36,7 +37,6 @@ rescue LoadError
|
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
39
|
-
|
40
40
|
task :default => :test
|
41
41
|
|
42
42
|
require 'rake/rdoctask'
|
@@ -47,7 +47,12 @@ Rake::RDocTask.new do |rdoc|
|
|
47
47
|
else
|
48
48
|
version = ""
|
49
49
|
end
|
50
|
-
|
50
|
+
# rdoc --help --webcvs, -W url
|
51
|
+
# github_blob_url = "<%= github_url %>/blob/v#{version}/%s"
|
52
|
+
# github_raw_url = "<%= github_url %>/raw/v#{version}/%s"
|
53
|
+
# github_blob_url = "http://github.com/dreamcat4/libxml-bindings/blob/v#{version}/%s"
|
54
|
+
# github_raw_url = "http://github.com/dreamcat4/libxml-bindings/raw/v#{version}/%s"
|
55
|
+
# rdoc.options = ["--webcvs", "#{github_blob_url}"]
|
51
56
|
rdoc.rdoc_dir = 'rdoc'
|
52
57
|
rdoc.title = "libxml-bindings #{version}"
|
53
58
|
rdoc.rdoc_files.include('README*')
|
data/VERSION.yml
CHANGED
data/lib/libxml_bindings.rb
CHANGED
@@ -1,88 +1,229 @@
|
|
1
1
|
require "xml/libxml"
|
2
2
|
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
3
|
+
class Module #:nodoc:
|
4
|
+
alias_method :method_alias, :alias_method
|
5
|
+
end
|
6
|
+
|
7
|
+
module LibxmlBindings #:nodoc:
|
8
|
+
|
9
|
+
module XML #:nodoc:
|
10
|
+
module Document
|
11
|
+
|
12
|
+
# :call-seq:
|
13
|
+
# doc.last = node -> XML::Node
|
14
|
+
#
|
15
|
+
# Appends +node+ to the end of the list of nodes at this level
|
16
|
+
def last=(node)
|
17
|
+
self.last? ? self.root.sibling=node : self.root = node
|
18
|
+
end
|
19
|
+
|
20
|
+
# :call-seq:
|
21
|
+
# doc.node["/xpath"] -> XML::Node
|
22
|
+
#
|
23
|
+
# Returns the first Node object matching +"/xpath"+, or +nil+ if no object could not be found.
|
24
|
+
#
|
25
|
+
# *Note* its usually recommended to remove all namespaces with strip! before xpath traversal.
|
26
|
+
def node()
|
27
|
+
self.root.node()
|
28
|
+
end
|
29
|
+
|
30
|
+
def node_at(xpath) #:nodoc:
|
31
|
+
self.root.node_at(xpath)
|
32
|
+
end
|
33
|
+
|
34
|
+
# :call-seq:
|
35
|
+
# doc.nodes["/xpath"] -> [XML::Node, XML::Node, XML::Node, ...]
|
36
|
+
#
|
37
|
+
# Returns an array containing all nodes matching +"/xpath"+, or +nil+ if no object could not be found.
|
38
|
+
#
|
39
|
+
# *Note* its usually recommended to remove all namespaces with strip! before xpath traversal.
|
40
|
+
def nodes()
|
41
|
+
self.root.nodes()
|
42
|
+
end
|
43
|
+
|
44
|
+
def nodes_at(xpath) #:nodoc:
|
45
|
+
self.root.search(xpath)
|
46
|
+
end
|
47
|
+
|
48
|
+
# :call-seq:
|
49
|
+
# doc.strip! -> XML::Document
|
50
|
+
# doc.strip_namespaces! -> XML::Document
|
51
|
+
#
|
52
|
+
# Strip all namespaces from this XML::Document.
|
53
|
+
# replaces the root node with a stripped XML::Node (and children)
|
54
|
+
#
|
55
|
+
# *Note* Xml elements with a namespace context are usually omited from xpath search criteria.
|
56
|
+
#
|
57
|
+
# See: http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/XPath.html
|
58
|
+
def strip!
|
59
|
+
# Parse xml
|
60
|
+
xml_str = self.to_s
|
61
|
+
# puts "xml_str=#{xml_str}\\n"
|
62
|
+
xml_str.gsub! /\ ?(soap|xmlns)(:\w*)?=\"[^\"]*\"/, ""
|
63
|
+
xml_str.gsub! /\<\w*:/, "<"
|
64
|
+
xml_str.gsub! /\<\/\w*:/, "</"
|
65
|
+
# puts "xml_str=#{xml_str}\\n"
|
66
|
+
xml_doc_stripped = xml_str.to_xmldoc
|
67
|
+
self.root = xml_doc_stripped.root.copy(true)
|
68
|
+
return self
|
69
|
+
end
|
70
|
+
alias_method :strip_namespaces!, :strip!
|
17
71
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
72
|
+
# :call-seq:
|
73
|
+
# doc.to_xml -> String
|
74
|
+
#
|
75
|
+
# Alias for XML::Document.to_s[http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000458]
|
76
|
+
def to_xml
|
77
|
+
return self.to_s
|
24
78
|
end
|
79
|
+
# alias_method :to_xml, :to_s
|
80
|
+
|
25
81
|
end
|
26
|
-
return results
|
27
|
-
end
|
28
82
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
inner_xml
|
41
|
-
end
|
42
|
-
|
43
|
-
# return this node and its contents as an xml string
|
44
|
-
def to_xml
|
45
|
-
self.to_s
|
46
|
-
end
|
83
|
+
module Node
|
84
|
+
|
85
|
+
class SearchNodes #:nodoc:
|
86
|
+
def initialize(options)
|
87
|
+
@node = options[:node]
|
88
|
+
@return_many = options[:return_many]
|
89
|
+
end
|
90
|
+
def [](args)
|
91
|
+
@return_many ? @node.nodes_at(args) : @node.node_at(args)
|
92
|
+
end
|
93
|
+
end
|
47
94
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
95
|
+
# :call-seq:
|
96
|
+
# self.inner_html -> String
|
97
|
+
#
|
98
|
+
# Alias for XML::Node.inner_xml[http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Node.html#M000188]
|
99
|
+
def inner_html
|
100
|
+
self.inner_xml
|
101
|
+
end
|
102
|
+
# alias_method :inner_html, :inner_xml
|
103
|
+
|
104
|
+
# :call-seq:
|
105
|
+
# self.last = XML::Node -> XML::Node
|
106
|
+
#
|
107
|
+
# Alias for XML::Node.sibling=[http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Node.html#M000223]
|
108
|
+
def last=(node)
|
109
|
+
return self.sibling = node
|
110
|
+
end
|
111
|
+
# alias_method :last=, :sibling=
|
112
|
+
|
113
|
+
# :call-seq:
|
114
|
+
# self.node["/xpath"] -> XML::Node
|
115
|
+
#
|
116
|
+
# Returns the first Node object matching +"/xpath"+, or +nil+ if no object could not be found.
|
117
|
+
# The path searched is relative to the node from which node[] was called.
|
118
|
+
#
|
119
|
+
# *Note* its usually recommended to remove all namespaces with strip! before xpath traversal.
|
120
|
+
def node()
|
121
|
+
return SearchNodes.new(:node => self, :return_many => false)
|
59
122
|
end
|
123
|
+
|
124
|
+
def node_at(xpath) #:nodoc:
|
125
|
+
self.find_first(xpath)
|
126
|
+
end
|
127
|
+
|
128
|
+
# :call-seq:
|
129
|
+
# self.nodes["/xpath"] -> [XML::Node, XML::Node, XML::Node, ...]
|
130
|
+
#
|
131
|
+
# Returns an array containing all nodes matching +"/xpath"+, or +nil+ if no object could not be found.
|
132
|
+
# The path searched is relative to the node from which node[] was called.
|
133
|
+
#
|
134
|
+
# *Note* its usually recommended to remove all namespaces with strip! before xpath traversal.
|
135
|
+
def nodes()
|
136
|
+
return SearchNodes.new(:node => self, :return_many => true)
|
137
|
+
end
|
138
|
+
|
139
|
+
def nodes_at(xpath) #:nodoc:
|
140
|
+
results = self.find(xpath).to_a
|
141
|
+
if block_given?
|
142
|
+
results.each do |result|
|
143
|
+
yield result
|
144
|
+
end
|
145
|
+
end
|
146
|
+
return results
|
147
|
+
end
|
148
|
+
|
149
|
+
# # :call-seq:
|
150
|
+
# # self.replace!(other_node) -> XML::Node
|
151
|
+
# #
|
152
|
+
# # Replaces the current XML::Node with +other_node+.
|
153
|
+
# # remove! is called on self, and it's handle will become lost.
|
154
|
+
# def replace!(other_node)
|
155
|
+
# node_node_parent = parent if parent?
|
156
|
+
# new_node_doc = self.doc if ! parent?
|
157
|
+
# self.remove!
|
158
|
+
# doc.root = other_node if new_node_doc
|
159
|
+
# parent << other_node if parent
|
160
|
+
# return other_node
|
161
|
+
# end
|
162
|
+
|
163
|
+
# # :call-seq:
|
164
|
+
# # self.strip! -> XML::Node
|
165
|
+
# # self.strip_namespaces! -> XML::Node
|
166
|
+
# #
|
167
|
+
# # Strip namespaces from this XML::Node and all its children.
|
168
|
+
# # replaces this XML::Node with the stripped XML::Node (and children)
|
169
|
+
# #
|
170
|
+
# # *Note* Xml elements with a namespace context are usually omited from xpath search criteria.
|
171
|
+
# #
|
172
|
+
# # See: http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/XPath.html
|
173
|
+
# def strip!
|
174
|
+
# # Parse xml
|
175
|
+
# xml_str = self.to_s
|
176
|
+
# # puts "xml_str=#{xml_str}\\n"
|
177
|
+
# xml_str.gsub! /\ ?(soap|xmlns)(:\w*)?=\"[^\"]*\"/, ""
|
178
|
+
# xml_str.gsub! /\<\w*:/, "<"
|
179
|
+
# xml_str.gsub! /\<\/\w*:/, "</"
|
180
|
+
# # puts "xml_str=#{xml_str}\\n"
|
181
|
+
# xml_node_stripped = xml_str.to_xmldoc
|
182
|
+
# new_node = xml_doc_stripped.root.copy(true)
|
183
|
+
# self.replace new_node
|
184
|
+
# return new_node
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
# alias_method :strip_namespaces!, :strip!
|
188
|
+
|
189
|
+
# :call-seq:
|
190
|
+
# self.to_xml -> String
|
191
|
+
#
|
192
|
+
# Alias for XML::Node.to_s[http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Node.html#M000216]
|
193
|
+
def to_xml
|
194
|
+
return self.to_s
|
195
|
+
end
|
196
|
+
# alias_method :to_xml, :to_s
|
197
|
+
|
60
198
|
end
|
61
|
-
raise "No default namespace found"
|
62
|
-
end
|
63
|
-
|
64
|
-
# register a namespace, of the form "foo:http://example.com/ns"
|
65
|
-
def register_namespace(name_and_href)
|
66
|
-
(@default_namespaces ||= []) <<name_and_href
|
67
199
|
end
|
68
200
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
201
|
+
module String
|
202
|
+
# :call-seq:
|
203
|
+
# str.to_xmldoc -> XML::Document
|
204
|
+
#
|
205
|
+
# Returns an XML Document object, or +nil+ if str could not be parsed as valid xml.
|
206
|
+
#
|
207
|
+
# The resulting libxml instance can be parsed, manipulated, and converted back to a string.
|
208
|
+
def to_xmldoc
|
209
|
+
return XML::Parser.string(self).parse
|
210
|
+
end
|
75
211
|
end
|
76
212
|
|
77
|
-
alias_method :find_base, :find unless method_defined?(:find_base)
|
78
|
-
alias_method :find, :find_with_default_ns
|
79
|
-
alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
|
80
|
-
alias_method :find_first, :find_first_with_default_ns
|
81
|
-
|
82
213
|
end
|
83
214
|
|
84
|
-
|
85
|
-
|
86
|
-
|
215
|
+
# Mix into the standard ruby classes
|
216
|
+
module LibXML
|
217
|
+
module XML #:nodoc:
|
218
|
+
class Document #:nodoc:
|
219
|
+
include LibxmlBindings::XML::Document
|
220
|
+
end
|
221
|
+
|
222
|
+
class Node #:nodoc:
|
223
|
+
include LibxmlBindings::XML::Node
|
224
|
+
end
|
87
225
|
end
|
88
226
|
end
|
227
|
+
class String #:nodoc:
|
228
|
+
include LibxmlBindings::String
|
229
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,10 +1,39 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'shoulda'
|
4
|
+
# require 'woulda'
|
4
5
|
|
5
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
7
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
8
|
require 'libxml_bindings'
|
8
9
|
|
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
|
+
|
10
38
|
end
|
39
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestLibxmlBindings < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "check that libxml-bindings classes are loaded" do
|
6
|
+
assert LibxmlBindings::XmlDocument
|
7
|
+
assert LibxmlBindings::XmlNode
|
8
|
+
assert LibxmlBindings::String
|
9
|
+
end
|
10
|
+
|
11
|
+
should "check that modules are included" do
|
12
|
+
assert LibXML::XML::Document.included_modules.include? LibxmlBindings::XML::Document
|
13
|
+
assert LibXML::XML::Node.included_modules.include? LibxmlBindings::XML::Node
|
14
|
+
assert String.included_modules.include? LibxmlBindings::String
|
15
|
+
end
|
16
|
+
|
17
|
+
obj LibxmlBindings::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 LibxmlBindings::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 LibxmlBindings::XmlNode::SearchNodes::instance_methods do
|
30
|
+
should_include :[] # xml readers
|
31
|
+
end
|
32
|
+
|
33
|
+
obj LibxmlBindings::String::instance_methods do
|
34
|
+
should_include :to_xmldoc
|
35
|
+
end
|
36
|
+
|
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
|