Class: Nokogiri::XML::Node

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

Instance Method Summary collapse

Instance Method Details

#__replaceObject



121
# File 'lib/xml/smart.rb', line 121

alias __replace replace

#do_xinclude_manual(relative_to = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/xml/smart.rb', line 96

def do_xinclude_manual(relative_to=nil)
  path = ((relative_to || self.document.basepath.to_s) + '/').sub(/\/+$/,'/')
  ctx = XPathContext.new(self)
  ctx.register_namespaces "xi"=>"http://www.w3.org/2001/XInclude"
  ctx.evaluate('.//xi:include').each do |ele|
    name = ele.attributes['href'].value
    name = path + name if name !~ /^(https?:|ftp:)/
    content = open(name,{ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
    insert = begin 
      Nokogiri::XML::parse(content).root # {|config| config.noblanks.noent.strict }.root
    rescue
      content
    end
    insert['xml:base'] = name unless File.dirname(ele.attributes['href'].value) == '.'
    x = ele.replace insert
    if x.is_a? Nokogiri::XML::Element
      x.do_xinclude_manual(relative_to)
    else
      x.each do |n|
        n.do_xinclude_manual(relative_to) if n.is_a? Nokogiri::XML::Element
      end
    end
  end
end

#replace(node_or_tags) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/xml/smart.rb', line 122

def replace(node_or_tags)
  x = __replace(node_or_tags)
  x.xpath('.|.//*|.//@*').each do |rns|
    x.namespace_scopes.each do |nss|
      rns.namespace = nss if rns.namespace && rns.namespace.href == nss.href
    end
  end
  x
end

#xpath_experimentalObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/xml/smart.rb', line 132

def xpath_experimental
  return NodeSet.new(document) unless document
  @nsc ||= 0
  if @nsc != self.document.ns_counter
    @ctx = XPathContext.new(self)
    @ctx.register_namespaces(self.document.custom_namespace_prefixes.merge(self.document.user_custom_namespace_prefixes))
    @nsc = self.document.ns_counter
  end
  path = path.gsub(/xmlns:/, ' :') unless Nokogiri.uses_libxml?
  @ctx.evaluate(path)
end

#xpath_fast(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/xml/smart.rb', line 85

def xpath_fast(path)
  # return xpath(path,self.document.custom_namespace_prefixes.merge(self.document.user_custom_namespace_prefixes))
  return NodeSet.new(document) unless document

  ns = self.document.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

#xpath_plain(path) ⇒ Object



82
83
84
# File 'lib/xml/smart.rb', line 82

def xpath_plain(path)
  XPathContext.new(self).evaluate(path)
end