Skip to content

Commit eed5b11

Browse files
committed
do some kind of automatic namespace registration in xpath_eval(). This
works only for namespaces defined in the context node (eg docroot if no second argument is given. If one wants to use namespaces defined elsewhere or with different prefixes, you still have to use xpath_ns_register()
1 parent b1dab29 commit eed5b11

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

ext/domxml/php_domxml.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4735,9 +4735,9 @@ static void php_xpathptr_eval(INTERNAL_FUNCTION_PARAMETERS, int mode, int expr)
47354735
xmlXPathContextPtr ctxp;
47364736
xmlXPathObjectPtr xpathobjp;
47374737
xmlNode *contextnodep;
4738-
int ret, str_len;
4738+
int ret, str_len, nsNr;
47394739
char *str;
4740-
4740+
xmlNsPtr *namespaces;
47414741
contextnode = NULL;
47424742
contextnodep = NULL;
47434743

@@ -4761,6 +4761,26 @@ static void php_xpathptr_eval(INTERNAL_FUNCTION_PARAMETERS, int mode, int expr)
47614761
DOMXML_GET_OBJ(contextnodep, contextnode, le_domxmlnodep);
47624762
}
47634763
ctxp->node = contextnodep;
4764+
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+
}
47644784

47654785
#if defined(LIBXML_XPTR_ENABLED)
47664786
if (mode == PHP_XPTR) {
@@ -4884,20 +4904,25 @@ PHP_FUNCTION(xpath_register_ns)
48844904

48854905
int prefix_len, uri_len, result;
48864906
xmlXPathContextPtr ctxp;
4887-
char *prefix, *uri, *uri_static;
4907+
char *prefix, *uri;
48884908
zval *id;
48894909

48904910
DOMXML_PARAM_FOUR(ctxp, id, le_xpathctxp, "ss", &prefix, &prefix_len, &uri, &uri_len);
48914911

4892-
/* set the context node to NULL - what is a context node anyway? */
48934912
ctxp->node = NULL;
48944913

4914+
#ifdef CHREGU_0
4915+
/* this leads to memleaks... commenting it out, as it works for me without copying
4916+
it. chregu */
48954917
/*
48964918
this is a hack - libxml2 doesn't copy the URI, it simply uses the string
48974919
given in the parameter - which is normally deallocated after the function
48984920
*/
4899-
uri_static = estrndup(uri, uri_len);
4921+
uri_static = estrndup(uri, uri_len);
49004922
result = xmlXPathRegisterNs(ctxp, prefix, uri_static);
4923+
#endif
4924+
4925+
result = xmlXPathRegisterNs(ctxp, prefix, uri);
49014926

49024927
if (0 == result) {
49034928
RETURN_TRUE;

0 commit comments

Comments
 (0)