Class: Nokogiri::XML::Document

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

Instance Method Summary collapse

Instance Method Details

#custom_namespace_prefixesObject



69
70
71
# File 'lib/xml/smart.rb', line 69

def custom_namespace_prefixes
  @custom_namespace_prefixes || custom_namespace_prefixes_update
end

#custom_namespace_prefixes_updateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xml/smart.rb', line 37

def custom_namespace_prefixes_update
  result = {}

  diffs = []
  ns = self.xpath('//namespace::*').to_a.delete_if do |n| 
    if diffs.include? n.href
      true
    else  
      diffs.push(n.href).uniq!
      false
    end  
  end 

  de = ns.find_all{|n| n.prefix.nil?}
  if de.length == 1
    result['xmlns'] = de[0].href
  end
  if de.length > 1
    i = 0
    de.each do |n|
      unless result.has_value? n.href
        result["xmlns#{i}"] = n.href
        i += 1
      end  
    end
  end

  ns.find_all{|n| !n.prefix.nil? && !(n.prefix == 'xml')}.each do |n|
    result[n.prefix] = n.href
  end
  @custom_namespace_prefixes = result
end

#ns_counterObject



20
21
22
# File 'lib/xml/smart.rb', line 20

def ns_counter
  @ns_counter ||= 1
end

#ns_updateObject



23
24
25
26
27
# File 'lib/xml/smart.rb', line 23

def ns_update
  @ns_counter ||= 1
  @ns_counter += 1
  self
end

#user_custom_namespace_prefixesObject



72
73
74
# File 'lib/xml/smart.rb', line 72

def user_custom_namespace_prefixes
  @user_custom_namespace_prefixes ||= {}
end

#user_custom_namespace_prefixes=(h) ⇒ Object



75
76
77
# File 'lib/xml/smart.rb', line 75

def user_custom_namespace_prefixes=(h)
  @user_custom_namespace_prefixes = h
end

#xpath_fast(path) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/xml/smart.rb', line 29

def xpath_fast(path)
  ns = self.custom_namespace_prefixes.merge(self.document.user_custom_namespace_prefixes)
  ctx = XPathContext.new(self)
  ctx.register_namespaces(self.document.custom_namespace_prefixes.merge(self.document.user_custom_namespace_prefixes))
  path = path.gsub(/xmlns:/, ' :') unless Nokogiri.uses_libxml?
  ctx.evaluate(path)
end