Class: Nokogiri::XML::XPathContext

### Home Classes Methods

### In Files

- lib/nokogiri/xml/xpath_context.rb
- ext/nokogiri/html_document.c

### Parent

Object

### Methods

- ::new
- #evaluate
- #register_namespaces
- #register_ns
- #register_variable

### Files

- CHANGELOG.ja.rdoc
- CHANGELOG.rdoc
- C_CODING_STYLE.rdoc
- Manifest.txt
- README.ja.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Nokogiri
- Nokogiri::CSS
- Nokogiri::CSS::Node
- Nokogiri::CSS::Parser
- Nokogiri::CSS::SyntaxError
- Nokogiri::CSS::Tokenizer
- Nokogiri::CSS::Tokenizer::ScanError
- Nokogiri::Decorators
- Nokogiri::Decorators::Slop
- Nokogiri::EncodingHandler
- Nokogiri::HTML
- Nokogiri::HTML::Builder
- Nokogiri::HTML::Document
- Nokogiri::HTML::Document::EncodingReader
- Nokogiri::HTML::Document::EncodingReader::JumpSAXHandler
- Nokogiri::HTML::DocumentFragment
- Nokogiri::HTML::ElementDescription
- Nokogiri::HTML::EntityDescription
- Nokogiri::HTML::EntityLookup
- Nokogiri::HTML::SAX
- Nokogiri::HTML::SAX::Parser
- Nokogiri::HTML::SAX::ParserContext
- Nokogiri::HTML::SAX::PushParser
- Nokogiri::SyntaxError
- Nokogiri::XML
- Nokogiri::XML::Attr
- Nokogiri::XML::AttributeDecl
- Nokogiri::XML::Builder
- Nokogiri::XML::CDATA
- Nokogiri::XML::CharacterData
- Nokogiri::XML::Comment
- Nokogiri::XML::DTD
- Nokogiri::XML::Document
- Nokogiri::XML::DocumentFragment
- Nokogiri::XML::Element
- Nokogiri::XML::ElementContent
- Nokogiri::XML::ElementDecl
- Nokogiri::XML::EntityDecl
- Nokogiri::XML::EntityReference
- Nokogiri::XML::Namespace
- Nokogiri::XML::Node
- Nokogiri::XML::Node::SaveOptions
- Nokogiri::XML::NodeSet
- Nokogiri::XML::Notation
- Nokogiri::XML::PP
- Nokogiri::XML::PP::CharacterData
- Nokogiri::XML::PP::Node
- Nokogiri::XML::ParseOptions
- Nokogiri::XML::ProcessingInstruction
- Nokogiri::XML::Reader
- Nokogiri::XML::RelaxNG
- Nokogiri::XML::SAX
- Nokogiri::XML::SAX::Document
- Nokogiri::XML::SAX::Parser
- Nokogiri::XML::SAX::Parser::Attribute
- Nokogiri::XML::SAX::ParserContext
- Nokogiri::XML::SAX::PushParser
- Nokogiri::XML::Schema
- Nokogiri::XML::SyntaxError
- Nokogiri::XML::Text
- Nokogiri::XML::XPath
- Nokogiri::XML::XPath::SyntaxError
- Nokogiri::XML::XPathContext
- Nokogiri::XSLT
- Nokogiri::XSLT::Stylesheet
- XSD
- XSD::XMLParser
- XSD::XMLParser::Nokogiri
- Object

No matching classes.

# Nokogiri::XML::XPathContext

### Public Class Methods

new(node) click to toggle source

Create a new XPathContext with `node` as the reference point.

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

      xmlXPathInit();

      Data_Get_Struct(nodeobj, xmlNode, node);

      ctx = xmlXPathNewContext(node->doc);
      ctx->node = node;
      self = Data_Wrap_Struct(klass, 0, deallocate, ctx);
      /*rb_iv_set(self, "@xpath_handler", Qnil); */
      return self;
    }

### Public Instance Methods

evaluate(search_path, handler = nil) click to toggle source

Evaluate the `search_path` returning an XML::XPath object.

    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 *)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);

      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));
      }

      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:
          if(NULL == xpath->nodesetval) {
            thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
              DOC_RUBY_OBJECT(ctx->doc));
          } else {
            thing = Nokogiri_wrap_xml_node_set(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 = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
            DOC_RUBY_OBJECT(ctx->doc));
      }

      xmlXPathFreeNodeSetList(xpath);

      return thing;
    }

register_namespaces(namespaces) click to toggle 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.to_s.gsub(/.*:/,'') # strip off 'xmlns:' or 'xml:'
        register_ns(k, v)
      end
    end

register_ns(prefix, uri) click to toggle 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;
    }

register_variable(name, value) click to toggle source

Register the variable `name` with `value`.

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

       xmlValue = xmlXPathNewCString(StringValuePtr(value));

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

       return self;
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.