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.



53
54
55
56
57
58
59
60
61
62
63
# File 'ext/nokogiri/xml_xpath_context.c', line 53

static VALUE new(VALUE klass, VALUE nodeobj)
{
  xmlXPathInit();

  xmlNodePtr node ;
  Data_Get_Struct(nodeobj, xmlNode, node);

  xmlXPathContextPtr ctx = xmlXPathNewContext(node->doc);
  ctx->node = node ;
  return Data_Wrap_Struct(klass, 0, deallocate, ctx);
}

Instance Method Details

#evaluate(search_path) ⇒ Object

Evaluate the search_path returning an XML::XPath object.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'ext/nokogiri/xml_xpath_context.c', line 34

static VALUE evaluate(VALUE self, VALUE search_path)
{
  xmlXPathContextPtr ctx;
  Data_Get_Struct(self, xmlXPathContext, ctx);

  xmlChar* query = (xmlChar *)StringValuePtr(search_path);
  xmlXPathObjectPtr xpath = xmlXPathEvalExpression(query, ctx);
  if(xpath == NULL) {
    rb_raise(rb_eRuntimeError, "Couldn't evaluate expression '%s'", query);
  }
  return Nokogiri_wrap_xml_xpath(xpath);
}

#register_namespaces(namespaces) ⇒ Object



5
6
7
8
9
10
# File 'lib/nokogiri/xml/xpath_context.rb', line 5

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(prefix, uri) ⇒ Object

Register the namespace with prefix and uri.



16
17
18
19
20
21
22
23
24
25
26
# File 'ext/nokogiri/xml_xpath_context.c', line 16

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