Class: Nokogiri::XML::Node

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

Instance Method Summary collapse

Instance Method Details

#__replaceObject



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

alias __replace replace

#do_xinclude_manual(relative_to = nil) ⇒ Object



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

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
    is_path = name !~ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://}
    name = path + name if is_path && !name.start_with?('/')
    content = ::URI::open(name, is_path ? 'r' : {: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



126
127
128
129
130
131
132
133
134
# File 'lib/xml/smart.rb', line 126

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



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/xml/smart.rb', line 136

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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xml/smart.rb', line 87

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

  #@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

#xpath_plain(path) ⇒ Object



84
85
86
# File 'lib/xml/smart.rb', line 84

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