Class: OxMlk::XML::Node

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/oxmlk/xml.rb', line 15

def self.from(data)
  case data
  when XML::Document
    data.root
  when XML::Node
    data
  when File
    XML::Parser.io(data).parse.root
  when Pathname, URI
    XML::Parser.file(data.to_s).parse.root
  when String
    XML::Parser.string(data).parse.root
  else
    raise 'Invalid XML data'
  end
end

Instance Method Details

#default_namespace(name) ⇒ Object



53
54
55
# File 'lib/oxmlk/xml.rb', line 53

def default_namespace(name)
  "oxdefault:#{name}"
end

#namespaced(xpath) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/oxmlk/xml.rb', line 45

def namespaced(xpath)
  xpath.between('|') do |section|
    section.between('/') do |component|
      unspaced?(component) ? default_namespace(component) : component
    end
  end
end

#search(xpath) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/oxmlk/xml.rb', line 32

def search(xpath)
  begin
    if namespaces.default && !xpath.include?(':')
      find(namespaced(xpath),
           default_namespace(namespaces.default.href))
    else
      find(xpath)
    end
  rescue Exception => ex
    raise ex, xpath
  end
end

#unspaced?(component) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/oxmlk/xml.rb', line 57

def unspaced?(component)
  component =~ /\w+/ && !component.include?(':') && !component.starts_with?('@')
end