Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/gdor/indexer/nokogiri_xml_node_mixin.rb

Overview

Monkey patch for Nokogiri to cache xpath contexts and make things faster under jRuby

Instance Method Summary collapse

Instance Method Details

#xpath(*paths) ⇒ Object



7
8
9
10
11
12
13
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
# File 'lib/gdor/indexer/nokogiri_xml_node_mixin.rb', line 7

def xpath(*paths)
  return NodeSet.new(document) unless document

  paths, handler, ns, binds = extract_params(paths)

  sets = paths.map do |path|
    # if self.contexts[path]
    #  ctx = self.contexts[path]
    # else
    if @context
      ctx = @context
    else
      ctx = XPathContext.new(self)
      @context = ctx
    end
    ctx.register_namespaces(ns)
    path = path.gsub(/xmlns:/, ' :') unless Nokogiri.uses_libxml?
    binds.each do |key, value|
      ctx.register_variable key.to_s, value
    end if binds
    ctx.evaluate(path, handler)
  end

  return sets.first if sets.length == 1

  NodeSet.new(document) do |combined|
    sets.each do |set|
      set.each do |node|
        combined << node
      end
    end
  end
end