Class: Nokogiri::XML::XPathContext
- Inherits:
-
Object
- Object
- Nokogiri::XML::XPathContext
- Defined in:
- lib/nokogiri/xml/xpath_context.rb,
ext/nokogiri/xml_xpath_context.c
Class Method Summary collapse
-
.new(node) ⇒ Object
Create a new XPathContext with
node
as the reference point.
Instance Method Summary collapse
-
#evaluate(search_path, handler = nil) ⇒ Object
Evaluate the
search_path
returning an XML::XPath object. -
#register_namespaces(namespaces) ⇒ Object
Register namespaces in
namespaces
. -
#register_ns(prefix, uri) ⇒ Object
Register the namespace with
prefix
anduri
. -
#register_variable(name, value) ⇒ Object
Register the variable
name
withvalue
.
Class Method Details
.new(node) ⇒ Object
Create a new XPathContext with node
as the reference point.
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'ext/nokogiri/xml_xpath_context.c', line 346
static VALUE
new (VALUE klass, VALUE nodeobj)
{
xmlNodePtr node;
xmlXPathContextPtr ctx;
VALUE self;
Data_Get_Struct(nodeobj, xmlNode, node);
xmlXPathInit();
ctx = xmlXPathNewContext(node->doc);
ctx->node = node;
xmlXPathRegisterNs(ctx, NOKOGIRI_BUILTIN_PREFIX, NOKOGIRI_BUILTIN_URI);
xmlXPathRegisterFuncNS(ctx, (const xmlChar *)"css-class", NOKOGIRI_BUILTIN_URI,
xpath_builtin_css_class);
self = Data_Wrap_Struct(klass, 0, deallocate, ctx);
return self;
}
|
Instance Method Details
#evaluate(search_path, handler = nil) ⇒ Object
Evaluate the search_path
returning an XML::XPath object.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'ext/nokogiri/xml_xpath_context.c', line 274
static VALUE
evaluate(int argc, VALUE *argv, VALUE self)
{
VALUE search_path, xpath_handler;
VALUE thing = Qnil;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr xpath;
xmlChar *query;
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, lookup, (void *)xpath_handler);
}
xmlResetLastError();
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_raise);
/* For some reason, xmlXPathEvalExpression will blow up with a generic error */
/* when there is a non existent function. */
xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler);
xpath = xmlXPathEvalExpression(query, ctx);
xmlSetStructuredErrorFunc(NULL, NULL);
xmlSetGenericErrorFunc(NULL, NULL);
if (xpath == NULL) {
xmlErrorPtr error = xmlGetLastError();
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
}
assert(ctx->doc);
assert(DOC_RUBY_OBJECT_TEST(ctx->doc));
switch (xpath->type) {
case XPATH_STRING:
thing = NOKOGIRI_STR_NEW2(xpath->stringval);
xmlFree(xpath->stringval);
break;
case XPATH_NODESET:
thing = noko_xml_node_set_wrap(xpath->nodesetval,
DOC_RUBY_OBJECT(ctx->doc));
break;
case XPATH_NUMBER:
thing = rb_float_new(xpath->floatval);
break;
case XPATH_BOOLEAN:
thing = xpath->boolval == 1 ? Qtrue : Qfalse;
break;
default:
thing = noko_xml_node_set_wrap(NULL, DOC_RUBY_OBJECT(ctx->doc));
}
xmlXPathFreeNodeSetList(xpath);
return thing;
}
|
#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
.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'ext/nokogiri/xml_xpath_context.c', line 95
static VALUE
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
.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'ext/nokogiri/xml_xpath_context.c', line 114
static VALUE
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;
}
|