Blame doc/examples/xpath1.c

Packit Service a31ea6
/** 
Packit Service a31ea6
 * section: 	XPath
Packit Service a31ea6
 * synopsis: 	Evaluate XPath expression and prints result node set.
Packit Service a31ea6
 * purpose: 	Shows how to evaluate XPath expression and register 
Packit Service a31ea6
 *          	known namespaces in XPath context.
Packit Service a31ea6
 * usage:	xpath1 <xml-file> <xpath-expr> [<known-ns-list>]
Packit Service a31ea6
 * test:	xpath1 test3.xml '//child2' > xpath1.tmp && diff xpath1.tmp $(srcdir)/xpath1.res
Packit Service a31ea6
 * author: 	Aleksey Sanin
Packit Service a31ea6
 * copy: 	see Copyright for the status of this software.
Packit Service a31ea6
 */
Packit Service a31ea6
#include <stdlib.h>
Packit Service a31ea6
#include <stdio.h>
Packit Service a31ea6
#include <string.h>
Packit Service a31ea6
#include <assert.h>
Packit Service a31ea6
Packit Service a31ea6
#include <libxml/tree.h>
Packit Service a31ea6
#include <libxml/parser.h>
Packit Service a31ea6
#include <libxml/xpath.h>
Packit Service a31ea6
#include <libxml/xpathInternals.h>
Packit Service a31ea6
Packit Service a31ea6
#if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_SAX1_ENABLED)
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
static void usage(const char *name);
Packit Service a31ea6
int  execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList);
Packit Service a31ea6
int  register_namespaces(xmlXPathContextPtr xpathCtx, const xmlChar* nsList);
Packit Service a31ea6
void print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output);
Packit Service a31ea6
Packit Service a31ea6
int 
Packit Service a31ea6
main(int argc, char **argv) {
Packit Service a31ea6
    /* Parse command line and process file */
Packit Service a31ea6
    if((argc < 3) || (argc > 4)) {
Packit Service a31ea6
	fprintf(stderr, "Error: wrong number of arguments.\n");
Packit Service a31ea6
	usage(argv[0]);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    } 
Packit Service a31ea6
    
Packit Service a31ea6
    /* Init libxml */     
Packit Service a31ea6
    xmlInitParser();
Packit Service a31ea6
    LIBXML_TEST_VERSION
Packit Service a31ea6
Packit Service a31ea6
    /* Do the main job */
Packit Service a31ea6
    if(execute_xpath_expression(argv[1], BAD_CAST argv[2], (argc > 3) ? BAD_CAST argv[3] : NULL) < 0) {
Packit Service a31ea6
	usage(argv[0]);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /* Shutdown libxml */
Packit Service a31ea6
    xmlCleanupParser();
Packit Service a31ea6
    
Packit Service a31ea6
    /*
Packit Service a31ea6
     * this is to debug memory for regression tests
Packit Service a31ea6
     */
Packit Service a31ea6
    xmlMemoryDump();
Packit Service a31ea6
    return 0;
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * usage:
Packit Service a31ea6
 * @name:		the program name.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Prints usage information.
Packit Service a31ea6
 */
Packit Service a31ea6
static void 
Packit Service a31ea6
usage(const char *name) {
Packit Service a31ea6
    assert(name);
Packit Service a31ea6
    
Packit Service a31ea6
    fprintf(stderr, "Usage: %s <xml-file> <xpath-expr> [<known-ns-list>]\n", name);
Packit Service a31ea6
    fprintf(stderr, "where <known-ns-list> is a list of known namespaces\n");
Packit Service a31ea6
    fprintf(stderr, "in \"<prefix1>=<href1> <prefix2>=href2> ...\" format\n");
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * execute_xpath_expression:
Packit Service a31ea6
 * @filename:		the input XML filename.
Packit Service a31ea6
 * @xpathExpr:		the xpath expression for evaluation.
Packit Service a31ea6
 * @nsList:		the optional list of known namespaces in 
Packit Service a31ea6
 *			"<prefix1>=<href1> <prefix2>=href2> ..." format.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Parses input XML file, evaluates XPath expression and prints results.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 on success and a negative value otherwise.
Packit Service a31ea6
 */
Packit Service a31ea6
int 
Packit Service a31ea6
execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList) {
Packit Service a31ea6
    xmlDocPtr doc;
Packit Service a31ea6
    xmlXPathContextPtr xpathCtx; 
Packit Service a31ea6
    xmlXPathObjectPtr xpathObj; 
Packit Service a31ea6
    
Packit Service a31ea6
    assert(filename);
Packit Service a31ea6
    assert(xpathExpr);
Packit Service a31ea6
Packit Service a31ea6
    /* Load XML document */
Packit Service a31ea6
    doc = xmlParseFile(filename);
Packit Service a31ea6
    if (doc == NULL) {
Packit Service a31ea6
	fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
Packit Service a31ea6
	return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /* Create xpath evaluation context */
Packit Service a31ea6
    xpathCtx = xmlXPathNewContext(doc);
Packit Service a31ea6
    if(xpathCtx == NULL) {
Packit Service a31ea6
        fprintf(stderr,"Error: unable to create new XPath context\n");
Packit Service a31ea6
        xmlFreeDoc(doc); 
Packit Service a31ea6
        return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
    
Packit Service a31ea6
    /* Register namespaces from list (if any) */
Packit Service a31ea6
    if((nsList != NULL) && (register_namespaces(xpathCtx, nsList) < 0)) {
Packit Service a31ea6
        fprintf(stderr,"Error: failed to register namespaces list \"%s\"\n", nsList);
Packit Service a31ea6
        xmlXPathFreeContext(xpathCtx); 
Packit Service a31ea6
        xmlFreeDoc(doc); 
Packit Service a31ea6
        return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /* Evaluate xpath expression */
Packit Service a31ea6
    xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
Packit Service a31ea6
    if(xpathObj == NULL) {
Packit Service a31ea6
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
Packit Service a31ea6
        xmlXPathFreeContext(xpathCtx); 
Packit Service a31ea6
        xmlFreeDoc(doc); 
Packit Service a31ea6
        return(-1);
Packit Service a31ea6
    }
Packit Service a31ea6
Packit Service a31ea6
    /* Print results */
Packit Service a31ea6
    print_xpath_nodes(xpathObj->nodesetval, stdout);
Packit Service a31ea6
Packit Service a31ea6
    /* Cleanup */
Packit Service a31ea6
    xmlXPathFreeObject(xpathObj);
Packit Service a31ea6
    xmlXPathFreeContext(xpathCtx); 
Packit Service a31ea6
    xmlFreeDoc(doc); 
Packit Service a31ea6
    
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * register_namespaces:
Packit Service a31ea6
 * @xpathCtx:		the pointer to an XPath context.
Packit Service a31ea6
 * @nsList:		the list of known namespaces in 
Packit Service a31ea6
 *			"<prefix1>=<href1> <prefix2>=href2> ..." format.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Registers namespaces from @nsList in @xpathCtx.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Returns 0 on success and a negative value otherwise.
Packit Service a31ea6
 */
Packit Service a31ea6
int 
Packit Service a31ea6
register_namespaces(xmlXPathContextPtr xpathCtx, const xmlChar* nsList) {
Packit Service a31ea6
    xmlChar* nsListDup;
Packit Service a31ea6
    xmlChar* prefix;
Packit Service a31ea6
    xmlChar* href;
Packit Service a31ea6
    xmlChar* next;
Packit Service a31ea6
    
Packit Service a31ea6
    assert(xpathCtx);
Packit Service a31ea6
    assert(nsList);
Packit Service a31ea6
Packit Service a31ea6
    nsListDup = xmlStrdup(nsList);
Packit Service a31ea6
    if(nsListDup == NULL) {
Packit Service a31ea6
	fprintf(stderr, "Error: unable to strdup namespaces list\n");
Packit Service a31ea6
	return(-1);	
Packit Service a31ea6
    }
Packit Service a31ea6
    
Packit Service a31ea6
    next = nsListDup; 
Packit Service a31ea6
    while(next != NULL) {
Packit Service a31ea6
	/* skip spaces */
Packit Service a31ea6
	while((*next) == ' ') next++;
Packit Service a31ea6
	if((*next) == '\0') break;
Packit Service a31ea6
Packit Service a31ea6
	/* find prefix */
Packit Service a31ea6
	prefix = next;
Packit Service a31ea6
	next = (xmlChar*)xmlStrchr(next, '=');
Packit Service a31ea6
	if(next == NULL) {
Packit Service a31ea6
	    fprintf(stderr,"Error: invalid namespaces list format\n");
Packit Service a31ea6
	    xmlFree(nsListDup);
Packit Service a31ea6
	    return(-1);	
Packit Service a31ea6
	}
Packit Service a31ea6
	*(next++) = '\0';	
Packit Service a31ea6
	
Packit Service a31ea6
	/* find href */
Packit Service a31ea6
	href = next;
Packit Service a31ea6
	next = (xmlChar*)xmlStrchr(next, ' ');
Packit Service a31ea6
	if(next != NULL) {
Packit Service a31ea6
	    *(next++) = '\0';	
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	/* do register namespace */
Packit Service a31ea6
	if(xmlXPathRegisterNs(xpathCtx, prefix, href) != 0) {
Packit Service a31ea6
	    fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
Packit Service a31ea6
	    xmlFree(nsListDup);
Packit Service a31ea6
	    return(-1);	
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
    
Packit Service a31ea6
    xmlFree(nsListDup);
Packit Service a31ea6
    return(0);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
/**
Packit Service a31ea6
 * print_xpath_nodes:
Packit Service a31ea6
 * @nodes:		the nodes set.
Packit Service a31ea6
 * @output:		the output file handle.
Packit Service a31ea6
 *
Packit Service a31ea6
 * Prints the @nodes content to @output.
Packit Service a31ea6
 */
Packit Service a31ea6
void
Packit Service a31ea6
print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output) {
Packit Service a31ea6
    xmlNodePtr cur;
Packit Service a31ea6
    int size;
Packit Service a31ea6
    int i;
Packit Service a31ea6
    
Packit Service a31ea6
    assert(output);
Packit Service a31ea6
    size = (nodes) ? nodes->nodeNr : 0;
Packit Service a31ea6
    
Packit Service a31ea6
    fprintf(output, "Result (%d nodes):\n", size);
Packit Service a31ea6
    for(i = 0; i < size; ++i) {
Packit Service a31ea6
	assert(nodes->nodeTab[i]);
Packit Service a31ea6
	
Packit Service a31ea6
	if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
Packit Service a31ea6
	    xmlNsPtr ns;
Packit Service a31ea6
	    
Packit Service a31ea6
	    ns = (xmlNsPtr)nodes->nodeTab[i];
Packit Service a31ea6
	    cur = (xmlNodePtr)ns->next;
Packit Service a31ea6
	    if(cur->ns) { 
Packit Service a31ea6
	        fprintf(output, "= namespace \"%s\"=\"%s\" for node %s:%s\n", 
Packit Service a31ea6
		    ns->prefix, ns->href, cur->ns->href, cur->name);
Packit Service a31ea6
	    } else {
Packit Service a31ea6
	        fprintf(output, "= namespace \"%s\"=\"%s\" for node %s\n", 
Packit Service a31ea6
		    ns->prefix, ns->href, cur->name);
Packit Service a31ea6
	    }
Packit Service a31ea6
	} else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
Packit Service a31ea6
	    cur = nodes->nodeTab[i];   	    
Packit Service a31ea6
	    if(cur->ns) { 
Packit Service a31ea6
    	        fprintf(output, "= element node \"%s:%s\"\n", 
Packit Service a31ea6
		    cur->ns->href, cur->name);
Packit Service a31ea6
	    } else {
Packit Service a31ea6
    	        fprintf(output, "= element node \"%s\"\n", 
Packit Service a31ea6
		    cur->name);
Packit Service a31ea6
	    }
Packit Service a31ea6
	} else {
Packit Service a31ea6
	    cur = nodes->nodeTab[i];    
Packit Service a31ea6
	    fprintf(output, "= node \"%s\": type %d\n", cur->name, cur->type);
Packit Service a31ea6
	}
Packit Service a31ea6
    }
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
#else
Packit Service a31ea6
int main(void) {
Packit Service a31ea6
    fprintf(stderr, "XPath support not compiled in\n");
Packit Service a31ea6
    exit(1);
Packit Service a31ea6
}
Packit Service a31ea6
#endif