Class: Nokogiri::XML::Document

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

Instance Method Summary collapse

Instance Method Details

#basepathObject



34
35
36
# File 'lib/xml/smart.rb', line 34

def basepath
  @basepath || ''
end

#basepath=(path) ⇒ Object



31
32
33
# File 'lib/xml/smart.rb', line 31

def basepath=(path)
  @basepath = File.dirname(path.to_s)
end

#custom_namespace_prefixesObject



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

def custom_namespace_prefixes
  @custom_namespace_prefixes ||= nil
  @custom_namespace_prefixes || custom_namespace_prefixes_update
end

#custom_namespace_prefixes_updateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xml/smart.rb', line 47

def custom_namespace_prefixes_update
  result = {}
  nsall = {}
  nsde = {}

  self.xpath('//namespace::*').each do |n|
    unless n.prefix == 'xml'
      nsde[n.href] = n.prefix if n.prefix.nil?
      nsall[n.href] = n.prefix
    end
  end

  count = -1
  nsall.each do |k,v|
    if v.nil? && nsde.length == 1
      result['xmlns'] = k
    elsif v.nil? && nsde.length > 1
      result["xmlns#{count+=1}"] = k
    else
      result[v] = k
    end
  end
  @custom_namespace_prefixes = result
end

#ns_counterObject



22
23
24
# File 'lib/xml/smart.rb', line 22

def ns_counter
  @ns_counter ||= 1
end

#ns_updateObject



25
26
27
28
29
# File 'lib/xml/smart.rb', line 25

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

#user_custom_namespace_prefixesObject



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

def user_custom_namespace_prefixes
  @user_custom_namespace_prefixes ||= {}
end

#user_custom_namespace_prefixes=(h) ⇒ Object



78
79
80
# File 'lib/xml/smart.rb', line 78

def user_custom_namespace_prefixes=(h)
  @user_custom_namespace_prefixes = h
end

#xpath_fast(path) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/xml/smart.rb', line 38

def xpath_fast(path)
  #@ctx = XPathContext.new(self) unless defined? @ctx
  #ctx = @ctx
  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