Source

perl-XML-LibXML / xpath.c

#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include "dom.h"

/**
 * Most of the code is stolen from textXPath. 
 * The almost only thing I added, is the storeing of the data, so
 * we can access the data easily - or say more easiely than through
 * libxml2.
 **/

xmlNodeSetPtr
domXPathSelect( xmlNodePtr refNode, xmlChar * path ) {
  xmlNodeSetPtr rv ;
  
  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;

    /* prepare the xpath context */
    ctxt = xmlXPathNewContext( refNode->doc );
    ctxt->node = refNode;

    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;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.