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.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'ext/nokogiri/xml_xpath_context.c', line 281
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;
}
|
Instance Method Details
#evaluate(search_path, handler = nil) ⇒ Object
Evaluate the search_path
returning an XML::XPath object.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'ext/nokogiri/xml_xpath_context.c', line 202
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) ⇒ Object
Register namespaces in namespaces
7 8 9 10 11 12 |
# 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(prefix, uri) ⇒ Object
Register the namespace with prefix
and uri
.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'ext/nokogiri/xml_xpath_context.c', line 18
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) ⇒ Object
Register the variable name
with value
.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'ext/nokogiri/xml_xpath_context.c', line 36
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;
}
|