Class: Nokogiri::XML::XPathContext

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/xpath_context.rb,
ext/nokogiri/xml_xpath_context.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(node) ⇒ Object

Create a new XPathContext with node as the reference point.



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/nokogiri/xml_xpath_context.c', line 372

static VALUE
rb_xml_xpath_context_new(VALUE klass, VALUE nodeobj)
{
  xmlNodePtr node;
  xmlXPathContextPtr ctx;
  VALUE self;

  Noko_Node_Get_Struct(nodeobj, xmlNode, node);

#if LIBXML_VERSION < 21000
  /* deprecated in 40483d0 */
  xmlXPathInit();
#endif

  ctx = xmlXPathNewContext(node->doc);
  ctx->node = node;

  xmlXPathRegisterNs(ctx, NOKOGIRI_PREFIX, NOKOGIRI_URI);
  xmlXPathRegisterNs(ctx, NOKOGIRI_BUILTIN_PREFIX, NOKOGIRI_BUILTIN_URI);
  xmlXPathRegisterFuncNS(ctx, (const xmlChar *)"css-class", NOKOGIRI_BUILTIN_URI,
                         xpath_builtin_css_class);
  xmlXPathRegisterFuncNS(ctx, (const xmlChar *)"local-name-is", NOKOGIRI_BUILTIN_URI,
                         xpath_builtin_local_name_is);

  self = Data_Wrap_Struct(klass, 0, xml_xpath_context_deallocate, ctx);
  return self;
}

Instance Method Details

#evaluate(search_path, handler = nil) ⇒ Object

Evaluate the search_path returning an XML::XPath object.



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'ext/nokogiri/xml_xpath_context.c', line 320

static VALUE
rb_xml_xpath_context_evaluate(int argc, VALUE *argv, VALUE self)
{
  VALUE search_path, xpath_handler;
  VALUE retval = Qnil;
  xmlXPathContextPtr ctx;
  xmlXPathObjectPtr xpath;
  xmlChar *query;
  VALUE errors = rb_ary_new();

  Data_Get_Struct(self, xmlXPathContext, ctx);

  if (rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1) {
    xpath_handler = Qnil;
  }

  query = (xmlChar *)StringValueCStr(search_path);

  if (Qnil != xpath_handler) {
    /* FIXME: not sure if this is the correct place to shove private data. */
    ctx->userData = (void *)xpath_handler;
    xmlXPathRegisterFuncLookup(ctx, handler_lookup, (void *)xpath_handler);
  }

  xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);
  xmlSetGenericErrorFunc((void *)errors, generic_exception_pusher);

  xpath = xmlXPathEvalExpression(query, ctx);

  xmlSetStructuredErrorFunc(NULL, NULL);
  xmlSetGenericErrorFunc(NULL, NULL);

  if (xpath == NULL) {
    rb_exc_raise(rb_ary_entry(errors, 0));
  }

  retval = xpath2ruby(xpath, ctx);
  if (retval == Qundef) {
    retval = noko_xml_node_set_wrap(NULL, DOC_RUBY_OBJECT(ctx->doc));
  }

  xmlXPathFreeNodeSetList(xpath);

  return retval;
}

#register_namespaces(namespaces) ⇒ Object

Register namespaces in namespaces



8
9
10
11
12
13
# File 'lib/nokogiri/xml/xpath_context.rb', line 8

def register_namespaces(namespaces)
  namespaces.each do |k, v|
    k = k.to_s.gsub(/.*:/, "") # strip off 'xmlns:' or 'xml:'
    register_ns(k, v)
  end
end

#register_ns(prefix, uri) ⇒ Object

Register the namespace with prefix and uri.



117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/nokogiri/xml_xpath_context.c', line 117

static VALUE
rb_xml_xpath_context_register_ns(VALUE self, VALUE prefix, VALUE uri)
{
  xmlXPathContextPtr ctx;
  Data_Get_Struct(self, xmlXPathContext, ctx);

  xmlXPathRegisterNs(ctx,
                     (const xmlChar *)StringValueCStr(prefix),
                     (const xmlChar *)StringValueCStr(uri)
                    );
  return self;
}

#register_variable(name, value) ⇒ Object

Register the variable name with value.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/nokogiri/xml_xpath_context.c', line 136

static VALUE
rb_xml_xpath_context_register_variable(VALUE self, VALUE name, VALUE value)
{
  xmlXPathContextPtr ctx;
  xmlXPathObjectPtr xmlValue;
  Data_Get_Struct(self, xmlXPathContext, ctx);

  xmlValue = xmlXPathNewCString(StringValueCStr(value));

  xmlXPathRegisterVariable(ctx,
                           (const xmlChar *)StringValueCStr(name),
                           xmlValue
                          );

  return self;
}