Class: XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/libxml_ruby_dc4.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_namespacesObject

Open up XML::Node from libxml and add convenience methods inspired by hpricot. (code.whytheluckystiff.net/hpricot/wiki/HpricotBasics) Also:

* provide better handling of default namespaces

an array of default namespaces to past into



10
11
12
# File 'lib/libxml_ruby_dc4.rb', line 10

def default_namespaces
  @default_namespaces
end

Instance Method Details

#/(xpath) ⇒ Object

alias for search



28
29
30
# File 'lib/libxml_ruby_dc4.rb', line 28

def /(xpath)
  search(xpath)
end

#at(xpath) ⇒ Object

find the child node with the given xpath



12
13
14
# File 'lib/libxml_ruby_dc4.rb', line 12

def at(xpath)
  self.find_first(xpath)
end

#find_first_with_default_ns(xpath_expr, namespace = nil) ⇒ Object Also known as: find_first



71
72
73
# File 'lib/libxml_ruby_dc4.rb', line 71

def find_first_with_default_ns(xpath_expr, namespace=nil)
  find_first_base(xpath_expr, namespace || default_namespaces)
end

#find_with_default_ns(xpath_expr, namespace = nil) ⇒ Object Also known as: find



67
68
69
# File 'lib/libxml_ruby_dc4.rb', line 67

def find_with_default_ns(xpath_expr, namespace=nil)
  find_base(xpath_expr, namespace || default_namespaces)
end

#inner_htmlObject

alias for inner_xml



37
38
39
# File 'lib/libxml_ruby_dc4.rb', line 37

def inner_html
  inner_xml
end

#inner_xmlObject

return the inner contents of this node as a string



32
33
34
# File 'lib/libxml_ruby_dc4.rb', line 32

def inner_xml
  child.to_s
end

#register_default_namespace(name) ⇒ Object

provide a name for the default namespace



52
53
54
55
56
57
58
59
60
# File 'lib/libxml_ruby_dc4.rb', line 52

def register_default_namespace(name)
  self.namespace.each do |n|
    if n.to_s == nil
      register_namespace("#{name}:#{n.href}")
      return
    end
  end
  raise "No default namespace found"
end

#register_namespace(name_and_href) ⇒ Object

register a namespace, of the form “foo:example.com/ns



63
64
65
# File 'lib/libxml_ruby_dc4.rb', line 63

def register_namespace(name_and_href)
  (@default_namespaces ||= []) <<name_and_href
end

#search(xpath) ⇒ Object

find the array of child nodes matching the given xpath



17
18
19
20
21
22
23
24
25
# File 'lib/libxml_ruby_dc4.rb', line 17

def search(xpath)
  results = self.find(xpath).to_a
  if block_given?
    results.each do |result|
      yield result
    end
  end
  return results
end

#to_xmlObject

return this node and its contents as an xml string



42
43
44
# File 'lib/libxml_ruby_dc4.rb', line 42

def to_xml
  self.to_s
end

#xpathObject

alias for path



47
48
49
# File 'lib/libxml_ruby_dc4.rb', line 47

def xpath
  self.path
end