Source

perl-XML-LibXML / xpath.c

Diff from to
  * libxml2.
  **/
 
+xmlXPathObjectPtr
+domXPathFind( xmlNodePtr refNode, xmlChar * path ) {
+    xmlNodeSetPtr rv ;
+    xmlXPathObjectPtr res = NULL;
+  
+    rv = xmlXPathNodeSetCreate( 0 );
+  
+    if ( refNode != NULL && refNode->doc != NULL && path != NULL ) {
+        /* we can only do a path in a valid document! 
+         */
+        xmlXPathContextPtr ctxt;
+        xmlXPathCompExprPtr comp;
+    
+        /* prepare the xpath context */
+        ctxt = xmlXPathNewContext( refNode->doc );
+        ctxt->node = refNode;
+    
+        comp = xmlXPathCompile( path );
+        if (comp != NULL) {
+            res = xmlXPathCompiledEval(comp, ctxt);
+            xmlXPathFreeCompExpr(comp);
+        }
+        
+        xmlXPathFreeContext(ctxt);
+    }
+    return res;
+}
+
 xmlNodeSetPtr
 domXPathSelect( xmlNodePtr refNode, xmlChar * path ) {
-  xmlNodeSetPtr rv ;
+    xmlNodeSetPtr rv ;
+    xmlXPathObjectPtr res;
   
-  rv = xmlXPathNodeSetCreate( 0 );
-  
-  if ( refNode != 0 && refNode->doc != 0 && path != 0 ) {
-    /* we can only do a path in a valid document! 
-     */
-    xmlXPathObjectPtr res;
-    xmlXPathContextPtr ctxt;
-    xmlXPathCompExprPtr comp;
+    rv = xmlXPathNodeSetCreate( 0 );
+    
+    res = domXPathFind( refNode, path );
+    
+    if (res != NULL) {
+            /* here we have to transfer the result from the internal
+               structure to the return value */
+        	/* get the result from the query */
+        	/* we have to unbind the nodelist, so free object can 
+        	   not kill it */
+        rv = res->nodesetval;  
+        res->nodesetval = 0 ;
+    
+    }
 
-    /* prepare the xpath context */
-    ctxt = xmlXPathNewContext( refNode->doc );
-    ctxt->node = refNode;
+    xmlXPathFreeObject(res);
 
-    comp = xmlXPathCompile( path );
-    if (comp != NULL) {
-      res = xmlXPathCompiledEval(comp, ctxt);
-      xmlXPathFreeCompExpr(comp);
-
-      /* here we have to transfer the result from the internal
-         structure to the return value */
-      if ( res != NULL ) {
-	/* get the result from the query */
-	/* we have to unbind the nodelist, so free object can 
-	   not kill it */
-	rv = res->nodesetval;  
-	res->nodesetval = 0 ;
-
-      }
-    }
-    else {
-      res = NULL;
-    }
-    
-    xmlXPathFreeObject(res);
-    xmlXPathFreeContext(ctxt);
-  }
-
-  return rv;
+    return rv;
 }