Class: XMLRowFinder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, debug: false) ⇒ XMLRowFinder

Returns a new instance of XMLRowFinder.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xml_row_finder.rb', line 14

def initialize(s, debug: false)

  @debug = debug
  doc = Rexle.new(s)
  @doc2 = Document.new(s)

  a = []

  doc.root.each_recursive do |e|
    e.attributes.delete
    a << e.backtrack.to_xpath
  end

  @to_a = a2 = a.map {|e| [a.count(e), e] }
  xpath = a2.max_by(&:first).last

  a3 = xpath.split('/')
  a4 = [xpath]
  p1 = []

  until (a3.length < 1) do
    p1 << a3.pop; a4 << a3.join('/') + "[%s]" % p1.reverse.join('/')
  end

  a5 = a4[0..-2].map do |xpath2|
    [XPath.match(@doc2, xpath2).length, xpath2]
  end

  @xpath = a5.reverse.detect {|num, xpath2| num > 1}.last

end

Instance Attribute Details

#to_aObject (readonly)

Returns the value of attribute to_a.



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

def to_a
  @to_a
end

Instance Method Details

#rowsObject



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

def rows()
  XPath.match(@doc2, @xpath)
end

#to_xpathObject



50
51
52
# File 'lib/xml_row_finder.rb', line 50

def to_xpath()
  @xpath
end