Module: Relaton::ElementFinder

Included in:
Bibcollection
Defined in:
lib/relaton/element_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/relaton/element_finder.rb', line 3

def document
  @document
end

Instance Method Details

#apply_namespace(xpath) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/relaton/element_finder.rb', line 17

def apply_namespace(xpath)
  xpath
    .gsub(%r{/([a-zA-Z])}, "/xmlns:\\1")
    .gsub(%r{::([a-zA-Z])}, "::xmlns:\\1")
    .gsub(%r{\[([a-zA-Z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1")
    .gsub(%r{\[([a-zA-Z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
end

#find(xpath, element = nil) ⇒ Object



9
10
11
# File 'lib/relaton/element_finder.rb', line 9

def find(xpath, element = nil)
  (element || document).at(apply_namespace(xpath))
end

#find_text(xpath, element = nil) ⇒ Object



5
6
7
# File 'lib/relaton/element_finder.rb', line 5

def find_text(xpath, element = nil)
  find(xpath, element)&.text
end

#find_xpath(xpath, element = nil) ⇒ Object



13
14
15
# File 'lib/relaton/element_finder.rb', line 13

def find_xpath(xpath, element = nil)
  element&.xpath(apply_namespace(xpath))
end