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
40
41
42
43
44
45
46
|
# File 'lib/traject/macros/nokogiri_macros.rb', line 13
def (xpath, ns: {}, to_text: true)
if ns && ns.length > 0
namespaces = default_namespaces.merge(ns)
else
namespaces = default_namespaces
end
lambda do |record, accumulator|
result = record.xpath(xpath, namespaces)
if to_text
result = result.collect do |n|
if n.kind_of?(Nokogiri::XML::Attr)
n.value
else
n.xpath('.//text()').collect(&:text).tap do |arr|
arr.reject! { |s| s =~ (/\A\s+\z/) }
end.join(" ")
end
end
else
result = result.to_a
end
accumulator.concat result
end
end
|