Module: WCAPI::XPath

Included in:
GetLocationResponse, GetRecordResponse, OpenSearchResponse, Record, SruSearchResponse
Defined in:
lib/wcapi/xpath.rb

Instance Method Summary collapse

Instance Method Details

#get_attribute(node, attr_name) ⇒ Object

figure out an attribute



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wcapi/xpath.rb', line 114

def get_attribute(node, attr_name)
  case node.class.to_s
  when 'REXML::XML::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
      if defined?(node[attr_name])
         return node[attr_name]
      else
         return node.property(attr_name)
      end
    end
  end
  return nil
end

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

get text for first matching node



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wcapi/xpath.rb', line 46

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



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

def xpath_all(pdoc, path, namespace = '')
  case parser_type(pdoc)
  when 'libxml'
    if namespace!=""
       return pdoc.find(path, namespace) if pdoc.find(path, namespace)
    else
       return pdoc.find(path) 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(doc, path, pnamespace = '') ⇒ Object

get first matching node



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wcapi/xpath.rb', line 25

def xpath_first(doc, path, pnamespace = '')
  begin
    elements = xpath_all(doc, path, pnamespace)
    if elements != nil
      case parser_type(doc)
        when 'libxml'
            return elements.first
        when 'rexml'
            return elements[0]
        else
            return nil
      end
    else
       return nil
    end
  rescue
     return nil
  end
end

#xpath_get_all_text(doc) ⇒ Object

get text for element)



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wcapi/xpath.rb', line 81

def xpath_get_all_text(doc)
  begin
    case parser_type(doc)
    when 'libxml'
      return doc.content
    when 'rexml'
      return doc.text
    end
  rescue
    return nil
  end
end

#xpath_get_name(doc) ⇒ Object

get node/element name



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wcapi/xpath.rb', line 95

def xpath_get_name(doc)
  begin
    case parser_type(doc)
    when 'libxml'
      if doc.name != 'text'
        return doc.name
      else
        return nil
      end
    when 'rexml'
      return doc.name
    end
  rescue
    return nil
  end
end

#xpath_get_text(doc) ⇒ Object

get text for element)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wcapi/xpath.rb', line 59

def xpath_get_text(doc)
  begin
    case parser_type(doc)
    when 'libxml'
      if doc.text? == false
        return doc.content
      else
        return ""
      end
    when 'rexml'
      if doc.has_text? == true
        return doc.text
      else
        return ""
      end
    end
  rescue
    return ""
  end
end