Class: XML::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_namespacesObject

an array of default namespaces to past into



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

def default_namespaces
  @default_namespaces
end

Instance Method Details

#/(xpath) ⇒ Object

alias for search



31
32
33
# File 'lib/libxml_ext/libxml_helper.rb', line 31

def /(xpath)
  search(xpath)
end

#at(xpath) ⇒ Object

find the child node with the given xpath



15
16
17
# File 'lib/libxml_ext/libxml_helper.rb', line 15

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

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



75
76
77
# File 'lib/libxml_ext/libxml_helper.rb', line 75

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



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

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

#inner_htmlObject

alias for inner_xml



41
42
43
# File 'lib/libxml_ext/libxml_helper.rb', line 41

def inner_html
  inner_xml
end

#inner_xmlObject

return the inner contents of this node as a string



36
37
38
# File 'lib/libxml_ext/libxml_helper.rb', line 36

def inner_xml
  child.to_s
end

#register_default_namespace(name) ⇒ Object

provide a name for the default namespace



56
57
58
59
60
61
62
63
64
# File 'lib/libxml_ext/libxml_helper.rb', line 56

def register_default_namespace(name)
  default_namespace = namespace.detect { |n| n.to_s == nil }
  
  if default_namespace
    register_namespace("#{name}:#{default_namespace.href}")
  else
    raise "No default namespace found"
  end
end

#register_namespace(name_and_href) ⇒ Object

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



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

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



20
21
22
23
24
25
26
27
28
# File 'lib/libxml_ext/libxml_helper.rb', line 20

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



46
47
48
# File 'lib/libxml_ext/libxml_helper.rb', line 46

def to_xml
  self.to_s
end

#xpathObject

alias for path



51
52
53
# File 'lib/libxml_ext/libxml_helper.rb', line 51

def xpath
  self.path
end