xpather 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ext/xpather/xpather.c +51 -1
- metadata +1 -1
data/ext/xpather/xpather.c
CHANGED
@@ -9,6 +9,52 @@ static void xml_free(void *doc) {
|
|
9
9
|
xmlFreeDoc(doc);
|
10
10
|
}
|
11
11
|
|
12
|
+
VALUE get(VALUE self, VALUE xpathExpr)
|
13
|
+
{
|
14
|
+
VALUE results = rb_ary_new();
|
15
|
+
xmlDocPtr doc;
|
16
|
+
xmlXPathContextPtr xpathCtx;
|
17
|
+
xmlXPathObjectPtr xpathObj;
|
18
|
+
xmlNodeSetPtr nodes;
|
19
|
+
xmlNodePtr cur;
|
20
|
+
int size;
|
21
|
+
int i;
|
22
|
+
|
23
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
24
|
+
|
25
|
+
xpathCtx = xmlXPathNewContext(doc);
|
26
|
+
if (xpathCtx == NULL) {
|
27
|
+
rb_raise(rb_eArgError, "Error: unable to create new XPath context\n");
|
28
|
+
return Qnil;
|
29
|
+
}
|
30
|
+
|
31
|
+
xpathObj = xmlXPathEvalExpression(StringValueCStr(xpathExpr), xpathCtx);
|
32
|
+
if (xpathObj == NULL) {
|
33
|
+
rb_raise(rb_eArgError, "Error: unable to evaluate xpath expression \"%s\"\n", StringValueCStr(xpathExpr));
|
34
|
+
xmlXPathFreeContext(xpathCtx);
|
35
|
+
return Qnil;
|
36
|
+
}
|
37
|
+
|
38
|
+
nodes = xpathObj->nodesetval;
|
39
|
+
size = (nodes) ? nodes->nodeNr : 0;
|
40
|
+
|
41
|
+
if (size == 1) {
|
42
|
+
results = rb_str_new2(xmlNodeGetContent(nodes->nodeTab[0]));
|
43
|
+
} else if (size > 1) {
|
44
|
+
for (i = 0; i < size; ++i) {
|
45
|
+
cur = nodes->nodeTab[i];
|
46
|
+
rb_ary_push(results, rb_str_new2(xmlNodeGetContent(cur)));
|
47
|
+
}
|
48
|
+
} else {
|
49
|
+
return Qnil;
|
50
|
+
}
|
51
|
+
|
52
|
+
xmlXPathFreeObject(xpathObj);
|
53
|
+
xmlXPathFreeContext(xpathCtx);
|
54
|
+
|
55
|
+
return results;
|
56
|
+
}
|
57
|
+
|
12
58
|
VALUE search(VALUE self, VALUE xpathExpr)
|
13
59
|
{
|
14
60
|
VALUE results = rb_ary_new();
|
@@ -40,7 +86,10 @@ VALUE search(VALUE self, VALUE xpathExpr)
|
|
40
86
|
size = (nodes) ? nodes->nodeNr : 0;
|
41
87
|
|
42
88
|
if (size == 1) {
|
43
|
-
|
89
|
+
nodeBuffer = xmlBufferCreate();
|
90
|
+
xmlNodeDump(nodeBuffer, doc, nodes->nodeTab[0], 0, 1);
|
91
|
+
results = rb_str_new2(nodeBuffer->content);
|
92
|
+
xmlBufferFree(nodeBuffer);
|
44
93
|
} else if (size > 1) {
|
45
94
|
for (i = 0; i < size; ++i) {
|
46
95
|
nodeBuffer = xmlBufferCreate();
|
@@ -89,4 +138,5 @@ void Init_xpather(void)
|
|
89
138
|
rb_define_singleton_method(klass, "new", constructor, 1);
|
90
139
|
rb_define_method(klass, "initialize", initialize, 1);
|
91
140
|
rb_define_method(klass, "search", search, 1);
|
141
|
+
rb_define_method(klass, "get", get, 1);
|
92
142
|
}
|