dreamcat4-libxml-ruby-dc4 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +2 -2
- data/lib/libxml_ruby_dc4.rb +88 -0
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/libxml_ruby_dc4.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
require "xml/libxml"
|
2
|
+
class XML::Node
|
3
|
+
##
|
4
|
+
# Open up XML::Node from libxml and add convenience methods inspired
|
5
|
+
# by hpricot.
|
6
|
+
# (http://code.whytheluckystiff.net/hpricot/wiki/HpricotBasics)
|
7
|
+
# Also:
|
8
|
+
# * provide better handling of default namespaces
|
9
|
+
# an array of default namespaces to past into
|
10
|
+
attr_accessor :default_namespaces
|
11
|
+
# find the child node with the given xpath
|
12
|
+
def at(xpath)
|
13
|
+
self.find_first(xpath)
|
14
|
+
end
|
15
|
+
|
16
|
+
# find the array of child nodes matching the given xpath
|
17
|
+
def search(xpath)
|
18
|
+
results = self.find(xpath).to_a
|
19
|
+
if block_given?
|
20
|
+
results.each do |result|
|
21
|
+
yield result
|
22
|
+
end
|
23
|
+
end
|
24
|
+
return results
|
25
|
+
end
|
26
|
+
|
27
|
+
# alias for search
|
28
|
+
def /(xpath)
|
29
|
+
search(xpath)
|
30
|
+
end
|
31
|
+
# return the inner contents of this node as a string
|
32
|
+
def inner_xml
|
33
|
+
child.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
# alias for inner_xml
|
37
|
+
def inner_html
|
38
|
+
inner_xml
|
39
|
+
end
|
40
|
+
|
41
|
+
# return this node and its contents as an xml string
|
42
|
+
def to_xml
|
43
|
+
self.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
# alias for path
|
47
|
+
def xpath
|
48
|
+
self.path
|
49
|
+
end
|
50
|
+
|
51
|
+
# provide a name for the default namespace
|
52
|
+
def register_default_namespace(name)
|
53
|
+
self.namespace.each do |n|
|
54
|
+
if n.to_s == nil
|
55
|
+
register_namespace("#{name}:#{n.href}")
|
56
|
+
return
|
57
|
+
end
|
58
|
+
end
|
59
|
+
raise "No default namespace found"
|
60
|
+
end
|
61
|
+
|
62
|
+
# register a namespace, of the form "foo:http://example.com/ns"
|
63
|
+
def register_namespace(name_and_href)
|
64
|
+
(@default_namespaces ||= []) <<name_and_href
|
65
|
+
end
|
66
|
+
|
67
|
+
def find_with_default_ns(xpath_expr, namespace=nil)
|
68
|
+
find_base(xpath_expr, namespace || default_namespaces)
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_first_with_default_ns(xpath_expr, namespace=nil)
|
72
|
+
find_first_base(xpath_expr, namespace || default_namespaces)
|
73
|
+
end
|
74
|
+
|
75
|
+
alias_method :find_base, :find unless method_defined?(:find_base)
|
76
|
+
alias_method :find, :find_with_default_ns
|
77
|
+
alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
|
78
|
+
alias_method :find_first, :find_first_with_default_ns
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
class String
|
83
|
+
def to_libxml_doc
|
84
|
+
xp = XML::Parser.new
|
85
|
+
xp.string = self
|
86
|
+
return xp.parse
|
87
|
+
end
|
88
|
+
end
|