Module: SRU::XPath

Included in:
Response
Defined in:
lib/sru/xpath.rb

Instance Method Summary collapse

Instance Method Details

#get_attribute(node, attr_name) ⇒ Object

figure out an attribute



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sru/xpath.rb', line 44

def get_attribute(node, attr_name)
  case node.class.to_s
  when 'REXML::Element'
    return node.attribute(attr_name)
  when 'LibXML::XML::Node'
    #There has been a method shift between 0.5 and 0.7
    if defined?(node.property) == nil
      return node.attributes[attr_name]
    else
      return node.property(attr_name)
    end
    #begin
    #        return node.attributes[attr_name]
    #rescue
     #   return node.property(attr_name)
    #end 
  end
  return nil
end

#xpath(pdoc, path, namespace = '') ⇒ Object

get text for first matching node



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sru/xpath.rb', line 31

def xpath(pdoc, path, namespace = '')
  el = xpath_first(pdoc, path, namespace)
  return unless el
  case parser_type(pdoc)
  when 'libxml'
    return el.content
  when 'rexml'
    return el.text 
  end
  return nil
end

#xpath_all(pdoc, path, namespace = '') ⇒ Object

get all matching nodes



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sru/xpath.rb', line 5

def xpath_all(pdoc, path, namespace = '')
  case parser_type(pdoc)
  when 'libxml'
    if namespace!=""
       return pdoc.find(path, namespace).to_a if pdoc.find(path, namespace)
    else
 return pdoc.find(path).to_a if pdoc.find(path)
    end
  when 'rexml'
    if namespace!=""
       return REXML::XPath.match(pdoc, path, namespace)
    else
return REXML::XPath.match(pdoc, path);
    end
  end
  return []
end

#xpath_first(pdoc, path, namespace = '') ⇒ Object

get first matching node



24
25
26
27
28
# File 'lib/sru/xpath.rb', line 24

def xpath_first(pdoc, path, namespace = '')
  elements = xpath_all(pdoc, path, namespace )
  return elements[0] if elements != nil
  return nil
end