]> granicus.if.org Git - php/commitdiff
add support for querying nodes with xpath expressions.
authorSterling Hughes <sterling@php.net>
Mon, 26 May 2003 03:57:41 +0000 (03:57 +0000)
committerSterling Hughes <sterling@php.net>
Mon, 26 May 2003 03:57:41 +0000 (03:57 +0000)
ext/simplexml/php_simplexml.h
ext/simplexml/simplexml.c

index ebe165a8f54fdd85757b014f25cf0f031aa9c74d..2861ab9753c11226474494ab7c4daa35ba16d19e 100644 (file)
@@ -53,6 +53,7 @@ PHP_MINFO_FUNCTION(simplexml);
 typedef struct {
        zend_object zo;
        xmlDocPtr document;
+       xmlXPathContextPtr xpath;
        xmlNodePtr node;
 } php_sxe_object;
 
index 1674250ce4d0950e25ca11297a2a0551874ff96a..763410735430244e4f6718b909607d22dec26834 100644 (file)
@@ -344,12 +344,60 @@ sxe_method_get(zval *object, char *name, int len TSRMLS_DC)
 }
 /* }}} */
 
+/* {{{ simplexml_ce_xpath_search()
+ */
+static void 
+simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAMETERS)
+{
+       php_sxe_object    *sxe;
+       zval              *value;
+       char              *query;
+       int                query_len;
+       int                i;
+       xmlNodeSetPtr      result;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE) {
+               return;
+       }
+
+       sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+       if (!sxe->xpath) {
+               sxe->xpath = xmlXPathNewContext(sxe->document);
+       }
+       sxe->xpath->node = sxe->node;
+
+       result = xmlXPathEval(query, sxe->xpath)->nodesetval;
+       if (!result) {
+               RETURN_FALSE;
+       }
+
+       array_init(return_value);
+
+       for (i = 0; i < result->nodeNr; ++i) {
+               MAKE_STD_ZVAL(value);
+               if (!xmlStrcmp(result->nodeTab[i]->name, "text")) {
+                       _node_as_zval(sxe, result->nodeTab[i]->parent, value);
+               } else {
+                       _node_as_zval(sxe, result->nodeTab[i], value);
+               }
+               add_next_index_zval(return_value, value);
+       }
+       
+}
+/* }}} */
+
+
 /* {{{ sxe_call_method()
  */
 static int
 sxe_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS)
 {
-       RETVAL_NULL();
+       if (!strcmp(method, "xsearch")) {
+               simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+       } else {
+               RETVAL_NULL();
+       }
+
        return 1;
 }
 /* }}} */
@@ -658,7 +706,6 @@ function_entry simplexml_functions[] = {
        {NULL, NULL, NULL}
 };
 
-
 zend_module_entry simplexml_module_entry = {
        STANDARD_MODULE_HEADER,
        "simplexml",
@@ -675,7 +722,7 @@ zend_module_entry simplexml_module_entry = {
 #ifdef COMPILE_DL_SIMPLEXML
 ZEND_GET_MODULE(simplexml)
 #endif
-
+       
 /* {{{ PHP_MINIT_FUNCTION(simplexml)
  */
 PHP_MINIT_FUNCTION(simplexml)