Method: XPather#get

Defined in:
ext/xpather/xpather.c

#get(xpathExpr) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/xpather/xpather.c', line 34

VALUE get(VALUE self, VALUE xpathExpr)
{
  VALUE results = rb_ary_new();
  xmlDocPtr doc;
  xmlXPathObjectPtr xpathObj;
  xmlNodeSetPtr nodes;
  xmlNodePtr current;
  int size, i;

  Data_Get_Struct(self, xmlDoc, doc);

  xpathObj = eval_and_search(doc, StringValueCStr(xpathExpr));

  if (xpathObj == NULL) { return Qnil; }

  nodes = xpathObj->nodesetval;
  size = (nodes) ? nodes->nodeNr : 0;

  if (size == 0) { return Qnil; }

  if (size == 1) {
    results = rb_str_new2(xmlNodeGetContent(nodes->nodeTab[0]));
  } else {
    for (i = 0; i < size; i++) {
      current = nodes->nodeTab[i];
      rb_ary_push(results, rb_str_new2(xmlNodeGetContent(current)));
    }
  }

  xmlXPathFreeObject(xpathObj);

  return results;
}