Class: LibXML::XML::XPath::Context
- Inherits:
-
Object
- Object
- LibXML::XML::XPath::Context
- Defined in:
- ext/libxml/ruby_xml_xpath_context.c,
ext/libxml/ruby_xml_xpath_context.c
Overview
The XML::XPath::Context class is used to evaluate XPath expressions. Generally, you should not directly use this class, but instead use the XML::Document#find and XML::Node#find methods.
doc = XML::Document.string('<header>content</header>')
context = XPath::Context.new(doc)
context.node = doc.root
context.register_namespaces_from_node(doc.root)
nodes = context.find('/header')
Instance Method Summary collapse
-
#disable_cache ⇒ Object
Disables an XPath::Context’s built-in cache.
-
#doc ⇒ Object
Obtain the XML::Document this node belongs to.
-
#enable_cache(size = nil) ⇒ Object
Enables an XPath::Context’s built-in cache.
-
#find("xpath") ⇒ true|false|number|string|XML::XPath::Object
Executes the provided xpath function.
-
#XPath::Context.new(doc) ⇒ XPath::Context
constructor
Creates a new XPath context for the specified document.
-
#node=(node) ⇒ Object
Set the current node used by the XPath engine.
-
#register_namespace(prefix, uri) ⇒ Object
Register the specified namespace URI with the specified prefix in this context.
-
#register_namespaces(["prefix: uri"]) ⇒ self
Register the specified namespaces in this context.
-
#register_namespaces_from_node(node) ⇒ self
Helper method to read in namespaces defined on a node.
Constructor Details
#XPath::Context.new(doc) ⇒ XPath::Context
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 77
static VALUE rxml_xpath_context_initialize(VALUE self, VALUE document)
{
xmlDocPtr xdoc;
rxml_xpath_context *wrapper;
if (rb_obj_is_kind_of(document, cXMLDocument) != Qtrue)
{
rb_raise(rb_eTypeError, "Supplied argument must be a document or node.");
}
TypedData_Get_Struct(document, xmlDoc, &rxml_document_data_type, xdoc);
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
if (wrapper == NULL)
{
wrapper = ALLOC(rxml_xpath_context);
wrapper->xctxt = NULL;
wrapper->document = document;
RTYPEDDATA_DATA(self) = wrapper;
}
else if (wrapper->xctxt)
{
xmlXPathFreeContext(wrapper->xctxt);
}
wrapper->xctxt = xmlXPathNewContext(xdoc);
return self;
}
|
Instance Method Details
#disable_cache ⇒ Object
Disables an XPath::Context’s built-in cache.
378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 378
static VALUE
rxml_xpath_context_disable_cache(VALUE self)
{
rxml_xpath_context *wrapper;
xmlXPathContextPtr xctxt;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
xctxt = wrapper->xctxt;
if (xmlXPathContextSetCache(xctxt, 0, 0, 0) == -1)
rxml_raise(xmlGetLastError());
return self;
}
|
#doc ⇒ Object
Obtain the XML::Document this node belongs to.
113 114 115 116 117 118 119 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 113
static VALUE rxml_xpath_context_doc(VALUE self)
{
rxml_xpath_context *wrapper;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
if (wrapper == NULL) return Qnil;
return wrapper->document;
}
|
#enable_cache(size = nil) ⇒ Object
Enables an XPath::Context’s built-in cache. If the cache is enabled then XPath objects will be cached internally for reuse. The size parameter controls sets the maximum number of XPath objects that will be cached per XPath object type (node-set, string, number, boolean, and misc objects). Set size to nil to use the default cache size of 100.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 350
static VALUE
rxml_xpath_context_enable_cache(int argc, VALUE *argv, VALUE self)
{
rxml_xpath_context *wrapper;
xmlXPathContextPtr xctxt;
VALUE size;
int value = -1;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
xctxt = wrapper->xctxt;
if (rb_scan_args(argc, argv, "01", &size) == 1)
{
value = NUM2INT(size);
}
if (xmlXPathContextSetCache(xctxt, 1, value, 0) == -1)
rxml_raise(xmlGetLastError());
return self;
}
|
#find("xpath") ⇒ true|false|number|string|XML::XPath::Object
Executes the provided xpath function. The result depends on the execution of the xpath statement. It may be true, false, a number, a string or a node set.
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 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 309
static VALUE rxml_xpath_context_find(VALUE self, VALUE xpath_expr)
{
rxml_xpath_context *wrapper;
xmlXPathContextPtr xctxt;
xmlXPathObjectPtr xobject;
xmlXPathCompExprPtr xcompexpr;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
xctxt = wrapper->xctxt;
if (TYPE(xpath_expr) == T_STRING)
{
VALUE expression = rb_check_string_type(xpath_expr);
xobject = xmlXPathEval((xmlChar*) StringValueCStr(expression), xctxt);
}
else if (rb_obj_is_kind_of(xpath_expr, cXMLXPathExpression))
{
TypedData_Get_Struct(xpath_expr, xmlXPathCompExpr, &rxml_xpath_expression_data_type, xcompexpr);
xobject = xmlXPathCompiledEval(xcompexpr, xctxt);
}
else
{
rb_raise(rb_eTypeError,
"Argument should be an instance of a String or XPath::Expression");
}
return rxml_xpath_to_value(wrapper->document, xctxt, xobject);
}
|
#node=(node) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 288
static VALUE rxml_xpath_context_node_set(VALUE self, VALUE node)
{
rxml_xpath_context *wrapper;
xmlXPathContextPtr xctxt;
xmlNodePtr xnode;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
xctxt = wrapper->xctxt;
TypedData_Get_Struct(node, xmlNode, &rxml_node_data_type, xnode);
xctxt->node = xnode;
return node;
}
|
#register_namespace(prefix, uri) ⇒ Object
Register the specified namespace URI with the specified prefix in this context.
context.register_namespace('xi', 'http://www.w3.org/2001/XInclude')
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 130
static VALUE rxml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri)
{
rxml_xpath_context *wrapper;
xmlXPathContextPtr ctxt;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
ctxt = wrapper->xctxt;
/* Prefix could be a symbol. */
prefix = rb_obj_as_string(prefix);
if (xmlXPathRegisterNs(ctxt, (xmlChar*) StringValuePtr(prefix),
(xmlChar*) StringValuePtr(uri)) == 0)
{
return (Qtrue);
}
else
{
/* Should raise an exception, IMHO (whose?, why shouldnt it? -danj)*/
rb_warning("register namespace failed");
return (Qfalse);
}
}
|
#register_namespaces(["prefix: uri"]) ⇒ self
Register the specified namespaces in this context. There are three different forms that libxml accepts. These include a string, an array of strings, or a hash table:
context.register_namespaces('xi:http://www.w3.org/2001/XInclude')
context.register_namespaces(['xlink:http://www.w3.org/1999/xlink',
'xi:http://www.w3.org/2001/XInclude')
context.register_namespaces('xlink' => 'http://www.w3.org/1999/xlink',
'xi' => 'http://www.w3.org/2001/XInclude')
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 274 275 276 277 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 232
static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
{
char *cp;
long i;
VALUE rprefix, ruri;
rxml_xpath_context *wrapper;
xmlXPathContextPtr xctxt;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
xctxt = wrapper->xctxt;
/* Need to loop through the 2nd argument and iterate through the
* list of namespaces that we want to allow */
switch (TYPE(nslist))
{
case T_STRING:
cp = strchr(StringValuePtr(nslist), (int) ':');
if (cp == NULL)
{
rprefix = nslist;
ruri = Qnil;
}
else
{
rprefix = rb_str_new(StringValuePtr(nslist), (long) ((intptr_t) cp - (intptr_t)StringValuePtr(nslist)));
ruri = rxml_new_cstr((const xmlChar*)&cp[1], xctxt->doc->encoding);
}
/* Should test the results of this */
rxml_xpath_context_register_namespace(self, rprefix, ruri);
break;
case T_ARRAY:
for (i = 0; i < RARRAY_LEN(nslist); i++)
{
rxml_xpath_context_register_namespaces(self, RARRAY_PTR(nslist)[i]);
}
break;
case T_HASH:
rb_hash_foreach(nslist, iterate_ns_hash, self);
break;
default:
rb_raise(
rb_eArgError,
"Invalid argument type, only accept string, array of strings, or an array of arrays");
}
return self;
}
|
#register_namespaces_from_node(node) ⇒ self
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'ext/libxml/ruby_xml_xpath_context.c', line 162
static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
VALUE node)
{
rxml_xpath_context *wrapper;
xmlXPathContextPtr xctxt;
xmlNodePtr xnode;
xmlNsPtr *xnsArr;
TypedData_Get_Struct(self, rxml_xpath_context, &rxml_xpath_context_data_type, wrapper);
xctxt = wrapper->xctxt;
if (rb_obj_is_kind_of(node, cXMLDocument) == Qtrue)
{
xmlDocPtr xdoc;
TypedData_Get_Struct(node, xmlDoc, &rxml_document_data_type, xdoc);
xnode = xmlDocGetRootElement(xdoc);
}
else if (rb_obj_is_kind_of(node, cXMLNode) == Qtrue)
{
TypedData_Get_Struct(node, xmlNode, &rxml_node_data_type, xnode);
}
else
{
rb_raise(rb_eTypeError, "The first argument must be a document or node.");
}
xnsArr = xmlGetNsList(xnode->doc, xnode);
if (xnsArr)
{
xmlNsPtr xns = *xnsArr;
while (xns)
{
/* If there is no prefix, then this is the default namespace.
Skip it for now. */
if (xns->prefix)
{
VALUE prefix = rxml_new_cstr(xns->prefix, xctxt->doc->encoding);
VALUE uri = rxml_new_cstr(xns->href, xctxt->doc->encoding);
rxml_xpath_context_register_namespace(self, prefix, uri);
}
xns = xns->next;
}
xmlFree(xnsArr);
}
return self;
}
|