Skip to content

Commit a2ccb29

Browse files
committed
@- Added xpath_register_ns_auto([contextnode]) for automatically registering
@ namespace definitions (chregu) changed my mind. Automatic namesapce registration is not done within xpath_eval() anymore, but in a seperate function.
1 parent eed5b11 commit a2ccb29

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

ext/domxml/php_domxml.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static zend_function_entry domxml_functions[] = {
264264
PHP_FE(xpath_eval, NULL)
265265
PHP_FE(xpath_eval_expression, NULL)
266266
PHP_FE(xpath_register_ns, NULL)
267+
PHP_FE(xpath_register_ns_auto, NULL)
267268
PHP_FE(domxml_doc_get_elements_by_tagname, NULL)
268269
#endif
269270

@@ -489,6 +490,7 @@ static zend_function_entry php_xpathctx_class_functions[] = {
489490
PHP_FALIAS(xpath_eval, xpath_eval, NULL)
490491
PHP_FALIAS(xpath_eval_expression, xpath_eval_expression, NULL)
491492
PHP_FALIAS(xpath_register_ns, xpath_register_ns, NULL)
493+
PHP_FALIAS(xpath_register_ns_auto, xpath_register_ns_auto, NULL)
492494
{NULL, NULL, NULL}
493495
};
494496

@@ -4737,7 +4739,6 @@ static void php_xpathptr_eval(INTERNAL_FUNCTION_PARAMETERS, int mode, int expr)
47374739
xmlNode *contextnodep;
47384740
int ret, str_len, nsNr;
47394741
char *str;
4740-
xmlNsPtr *namespaces;
47414742
contextnode = NULL;
47424743
contextnodep = NULL;
47434744

@@ -4762,26 +4763,6 @@ static void php_xpathptr_eval(INTERNAL_FUNCTION_PARAMETERS, int mode, int expr)
47624763
}
47634764
ctxp->node = contextnodep;
47644765

4765-
/* automatic namespace definitions registration.
4766-
it's only done for the context node
4767-
if you need namespaces defined in other nodes,
4768-
you have to specify them explicitely with
4769-
xpath_register_ns();
4770-
*/
4771-
if (contextnodep) {
4772-
namespaces = xmlGetNsList(ctxp->doc, contextnodep);
4773-
} else {
4774-
namespaces = xmlGetNsList(ctxp->doc, xmlDocGetRootElement(ctxp->doc));
4775-
}
4776-
4777-
nsNr = 0;
4778-
if (namespaces != NULL) {
4779-
while (namespaces[nsNr] != NULL) {
4780-
xmlXPathRegisterNs(ctxp, namespaces[nsNr]->prefix, namespaces[nsNr]->href);
4781-
nsNr++;
4782-
}
4783-
}
4784-
47854766
#if defined(LIBXML_XPTR_ENABLED)
47864767
if (mode == PHP_XPTR) {
47874768
xpathobjp = xmlXPtrEval(BAD_CAST str, ctxp);
@@ -4897,11 +4878,6 @@ PHP_FUNCTION(xpath_eval_expression)
48974878
Registeres the given namespace in the passed XPath context */
48984879
PHP_FUNCTION(xpath_register_ns)
48994880
{
4900-
/*
4901-
TODO:
4902-
- automagically register all namespaces when creating a new context
4903-
*/
4904-
49054881
int prefix_len, uri_len, result;
49064882
xmlXPathContextPtr ctxp;
49074883
char *prefix, *uri;
@@ -4930,6 +4906,45 @@ PHP_FUNCTION(xpath_register_ns)
49304906
RETURN_FALSE;
49314907
}
49324908
/* }}} */
4909+
4910+
/* {{{ proto bool xpath_register_ns_auto([object xpathctx_handle,] [object contextnode])
4911+
Registeres the given namespace in the passed XPath context */
4912+
PHP_FUNCTION(xpath_register_ns_auto)
4913+
{
4914+
/* automatic namespace definitions registration.
4915+
it's only done for the context node
4916+
if you need namespaces defined in other nodes,
4917+
you have to specify them explicitely with
4918+
xpath_register_ns();
4919+
*/
4920+
4921+
zval *contextnode = NULL, *id;
4922+
xmlXPathContextPtr ctxp;
4923+
xmlNodePtr contextnodep;
4924+
xmlNsPtr *namespaces;
4925+
int nsNr;
4926+
4927+
DOMXML_PARAM_ONE(ctxp, id, le_xpathctxp, "|o", &contextnode);
4928+
4929+
if (contextnode == NULL) {
4930+
namespaces = xmlGetNsList(ctxp->doc, xmlDocGetRootElement(ctxp->doc));
4931+
} else {
4932+
DOMXML_GET_OBJ(contextnodep, contextnode, le_domxmlnodep);
4933+
namespaces = xmlGetNsList(ctxp->doc, contextnodep);
4934+
}
4935+
4936+
nsNr = 0;
4937+
if (namespaces != NULL) {
4938+
while (namespaces[nsNr] != NULL) {
4939+
xmlXPathRegisterNs(ctxp, namespaces[nsNr]->prefix, namespaces[nsNr]->href);
4940+
nsNr++;
4941+
}
4942+
}
4943+
4944+
RETURN_TRUE;
4945+
}
4946+
/* }}} */
4947+
49334948
#endif /* defined(LIBXML_XPATH_ENABLED) */
49344949

49354950
#if defined(LIBXML_XPTR_ENABLED)

ext/domxml/php_domxml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ PHP_FUNCTION(xpath_new_context);
214214
PHP_FUNCTION(xpath_eval);
215215
PHP_FUNCTION(xpath_eval_expression);
216216
PHP_FUNCTION(xpath_register_ns);
217+
PHP_FUNCTION(xpath_register_ns_auto);
217218
PHP_FUNCTION(domxml_doc_get_elements_by_tagname);
218219
PHP_FUNCTION(domxml_doc_get_element_by_id);
219220
#endif

0 commit comments

Comments
 (0)