Module: OAI::XPath

Instance Method Summary collapse

Instance Method Details

#get_attribute(node, attr_name) ⇒ Object

figure out an attribute



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/oai/xpath.rb', line 36

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
	  #node.property is being deprecated.  We'll eventually remove
	  #this trap
	  begin
 return node[attr_name]
      rescue
         return node.property(attr_name)
      end
    end	
  end
  return nil
end

#xpath(doc, path) ⇒ Object

get text for first matching node



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/oai/xpath.rb', line 23

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

#xpath_all(doc, path) ⇒ Object

get all matching nodes



5
6
7
8
9
10
11
12
13
# File 'lib/oai/xpath.rb', line 5

def xpath_all(doc, path)
  case parser_type(doc)
  when 'libxml'
    return doc.find(path).to_a if doc.find(path)
  when 'rexml'
    return REXML::XPath.match(doc, path)
  end
  return []
end

#xpath_first(doc, path) ⇒ Object

get first matching node



16
17
18
19
20
# File 'lib/oai/xpath.rb', line 16

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