Class Nokogiri::XML::XPathContext inherits from Object
Public Class Methods
- new(p1) Show Source
Create a new XPathContext with node as the reference point.
-
static VALUE new(VALUE klass, VALUE nodeobj) { xmlXPathInit(); xmlNodePtr node ; Data_Get_Struct(nodeobj, xmlNode, node); xmlXPathContextPtr ctx = xmlXPathNewContext(node->doc); ctx->node = node; VALUE self = Data_Wrap_Struct(klass, 0, deallocate, ctx); //rb_iv_set(self, "@xpath_handler", Qnil); return self; }
Public Instance Methods
- evaluate(...) Show Source
Evaluate the search_path returning an XML::XPath object.
-
static VALUE evaluate(int argc, VALUE *argv, VALUE self) { VALUE search_path, xpath_handler; xmlXPathContextPtr ctx; Data_Get_Struct(self, xmlXPathContext, ctx); if(rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1) xpath_handler = Qnil; xmlChar* query = (xmlChar *)StringValuePtr(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, xpath_exception_handler); // For some reason, xmlXPathEvalExpression will blow up with a generic error // when there is a non existent function. xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler); xmlXPathObjectPtr xpath = xmlXPathEvalExpression(query, ctx); xmlSetStructuredErrorFunc(NULL, NULL); xmlSetGenericErrorFunc(NULL, NULL); if(xpath == NULL) { VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath")); VALUE klass = rb_const_get(xpath, rb_intern("SyntaxError")); xmlErrorPtr error = xmlGetLastError(); rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error)); } VALUE xpath_object = Nokogiri_wrap_xml_xpath(xpath); assert(ctx->doc); assert(DOC_RUBY_OBJECT_TEST(ctx->doc)); rb_iv_set(xpath_object, "@document", DOC_RUBY_OBJECT(ctx->doc)); return xpath_object; }
- register_namespaces(namespaces) Show Source
Register namespaces in namespaces
-
# File lib/nokogiri/xml/xpath_context.rb, line 7 def register_namespaces(namespaces) namespaces.each do |k, v| k = k.gsub(/.*:/,'') # strip off 'xmlns:' or 'xml:' register_ns(k, v) end end
- register_ns(p1, p2) Show Source
Register the namespace with prefix and uri.
-
static VALUE register_ns(VALUE self, VALUE prefix, VALUE uri) { xmlXPathContextPtr ctx; Data_Get_Struct(self, xmlXPathContext, ctx); xmlXPathRegisterNs( ctx, (const xmlChar *)StringValuePtr(prefix), (const xmlChar *)StringValuePtr(uri) ); return self; }