Blame doc/examples/xpath2.c

Packit 423ecb
/** 
Packit 423ecb
 * section: 	XPath
Packit 423ecb
 * synopsis: 	Load a document, locate subelements with XPath, modify
Packit 423ecb
 *              said elements and save the resulting document.
Packit 423ecb
 * purpose: 	Shows how to make a full round-trip from a load/edit/save
Packit 423ecb
 * usage:	xpath2 <xml-file> <xpath-expr> <new-value>
Packit 423ecb
 * test:	xpath2 test3.xml '//discarded' discarded > xpath2.tmp && diff xpath2.tmp $(srcdir)/xpath2.res
Packit 423ecb
 * author: 	Aleksey Sanin and Daniel Veillard
Packit 423ecb
 * copy: 	see Copyright for the status of this software.
Packit 423ecb
 */
Packit 423ecb
#include <stdlib.h>
Packit 423ecb
#include <stdio.h>
Packit 423ecb
#include <string.h>
Packit 423ecb
#include <assert.h>
Packit 423ecb
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/xpath.h>
Packit 423ecb
#include <libxml/xpathInternals.h>
Packit 423ecb
Packit 423ecb
#if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_SAX1_ENABLED) && \
Packit 423ecb
    defined(LIBXML_OUTPUT_ENABLED)
Packit 423ecb
Packit 423ecb
Packit 423ecb
static void usage(const char *name);
Packit 423ecb
static int example4(const char *filename, const xmlChar * xpathExpr,
Packit 423ecb
                    const xmlChar * value);
Packit 423ecb
static void update_xpath_nodes(xmlNodeSetPtr nodes, const xmlChar * value);
Packit 423ecb
Packit 423ecb
Packit 423ecb
int 
Packit 423ecb
main(int argc, char **argv) {
Packit 423ecb
    /* Parse command line and process file */
Packit 423ecb
    if (argc != 4) {
Packit 423ecb
	fprintf(stderr, "Error: wrong number of arguments.\n");
Packit 423ecb
	usage(argv[0]);
Packit 423ecb
	return(-1);
Packit 423ecb
    } 
Packit 423ecb
    
Packit 423ecb
    /* Init libxml */     
Packit 423ecb
    xmlInitParser();
Packit 423ecb
    LIBXML_TEST_VERSION
Packit 423ecb
Packit 423ecb
    /* Do the main job */
Packit 423ecb
    if (example4(argv[1], BAD_CAST argv[2], BAD_CAST argv[3])) {
Packit 423ecb
	usage(argv[0]);
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /* Shutdown libxml */
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    
Packit 423ecb
    /*
Packit 423ecb
     * this is to debug memory for regression tests
Packit 423ecb
     */
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
    return 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * usage:
Packit 423ecb
 * @name:		the program name.
Packit 423ecb
 *
Packit 423ecb
 * Prints usage information.
Packit 423ecb
 */
Packit 423ecb
static void 
Packit 423ecb
usage(const char *name) {
Packit 423ecb
    assert(name);
Packit 423ecb
    
Packit 423ecb
    fprintf(stderr, "Usage: %s <xml-file> <xpath-expr> <value>\n", name);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * example4:
Packit 423ecb
 * @filename:		the input XML filename.
Packit 423ecb
 * @xpathExpr:		the xpath expression for evaluation.
Packit 423ecb
 * @value:		the new node content.
Packit 423ecb
 *
Packit 423ecb
 * Parses input XML file, evaluates XPath expression and update the nodes
Packit 423ecb
 * then print the result.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success and a negative value otherwise.
Packit 423ecb
 */
Packit 423ecb
static int 
Packit 423ecb
example4(const char* filename, const xmlChar* xpathExpr, const xmlChar* value) {
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
    xmlXPathContextPtr xpathCtx; 
Packit 423ecb
    xmlXPathObjectPtr xpathObj; 
Packit 423ecb
    
Packit 423ecb
    assert(filename);
Packit 423ecb
    assert(xpathExpr);
Packit 423ecb
    assert(value);
Packit 423ecb
Packit 423ecb
    /* Load XML document */
Packit 423ecb
    doc = xmlParseFile(filename);
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
	fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /* Create xpath evaluation context */
Packit 423ecb
    xpathCtx = xmlXPathNewContext(doc);
Packit 423ecb
    if(xpathCtx == NULL) {
Packit 423ecb
        fprintf(stderr,"Error: unable to create new XPath context\n");
Packit 423ecb
        xmlFreeDoc(doc); 
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
    
Packit 423ecb
    /* Evaluate xpath expression */
Packit 423ecb
    xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
Packit 423ecb
    if(xpathObj == NULL) {
Packit 423ecb
        fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
Packit 423ecb
        xmlXPathFreeContext(xpathCtx); 
Packit 423ecb
        xmlFreeDoc(doc); 
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /* update selected nodes */
Packit 423ecb
    update_xpath_nodes(xpathObj->nodesetval, value);
Packit 423ecb
Packit 423ecb
    
Packit 423ecb
    /* Cleanup of XPath data */
Packit 423ecb
    xmlXPathFreeObject(xpathObj);
Packit 423ecb
    xmlXPathFreeContext(xpathCtx); 
Packit 423ecb
Packit 423ecb
    /* dump the resulting document */
Packit 423ecb
    xmlDocDump(stdout, doc);
Packit 423ecb
Packit 423ecb
Packit 423ecb
    /* free the document */
Packit 423ecb
    xmlFreeDoc(doc); 
Packit 423ecb
    
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * update_xpath_nodes:
Packit 423ecb
 * @nodes:		the nodes set.
Packit 423ecb
 * @value:		the new value for the node(s)
Packit 423ecb
 *
Packit 423ecb
 * Prints the @nodes content to @output.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
update_xpath_nodes(xmlNodeSetPtr nodes, const xmlChar* value) {
Packit 423ecb
    int size;
Packit 423ecb
    int i;
Packit 423ecb
    
Packit 423ecb
    assert(value);
Packit 423ecb
    size = (nodes) ? nodes->nodeNr : 0;
Packit 423ecb
    
Packit 423ecb
    /*
Packit 423ecb
     * NOTE: the nodes are processed in reverse order, i.e. reverse document
Packit 423ecb
     *       order because xmlNodeSetContent can actually free up descendant
Packit 423ecb
     *       of the node and such nodes may have been selected too ! Handling
Packit 423ecb
     *       in reverse order ensure that descendant are accessed first, before
Packit 423ecb
     *       they get removed. Mixing XPath and modifications on a tree must be
Packit 423ecb
     *       done carefully !
Packit 423ecb
     */
Packit 423ecb
    for(i = size - 1; i >= 0; i--) {
Packit 423ecb
	assert(nodes->nodeTab[i]);
Packit 423ecb
	
Packit 423ecb
	xmlNodeSetContent(nodes->nodeTab[i], value);
Packit 423ecb
	/*
Packit 423ecb
	 * All the elements returned by an XPath query are pointers to
Packit 423ecb
	 * elements from the tree *except* namespace nodes where the XPath
Packit 423ecb
	 * semantic is different from the implementation in libxml2 tree.
Packit 423ecb
	 * As a result when a returned node set is freed when
Packit 423ecb
	 * xmlXPathFreeObject() is called, that routine must check the
Packit 423ecb
	 * element type. But node from the returned set may have been removed
Packit 423ecb
	 * by xmlNodeSetContent() resulting in access to freed data.
Packit 423ecb
	 * This can be exercised by running
Packit 423ecb
	 *       valgrind xpath2 test3.xml '//discarded' discarded
Packit 423ecb
	 * There is 2 ways around it:
Packit 423ecb
	 *   - make a copy of the pointers to the nodes from the result set 
Packit 423ecb
	 *     then call xmlXPathFreeObject() and then modify the nodes
Packit 423ecb
	 * or
Packit 423ecb
	 *   - remove the reference to the modified nodes from the node set
Packit 423ecb
	 *     as they are processed, if they are not namespace nodes.
Packit 423ecb
	 */
Packit 423ecb
	if (nodes->nodeTab[i]->type != XML_NAMESPACE_DECL)
Packit 423ecb
	    nodes->nodeTab[i] = NULL;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#else
Packit 423ecb
int main(void) {
Packit 423ecb
    fprintf(stderr, "XPath support not compiled in\n");
Packit 423ecb
    exit(1);
Packit 423ecb
}
Packit 423ecb
#endif