From: Gary Pennington (Gary.Pennington@uk.sun.com)
Date: Fri Feb 16 2001 - 06:01:27 EST
Hi Peter,
Find attached a very simple program that does use some XPath functions.
You should be able to compile it and by changing the values in the
xmlXPathEvalExpression functions do some XPath against your documents.
Gary
Peter Jacobi wrote:
> Dear Daniel, All,
>
> It would be rather helpful for me to see some example using XPath
> queries with libxml2 (not in the context of XSLT). So far I only found the
> commented xpath.h header in the documentation on xmlsoft.org.
>
> Can anybody provide a simple example program?
>
> Regards,
> Peter Jacobi
>
> ----
> Message from the list xml@rpmfind.net
> Archived at : http://xmlsoft.org/messages/
> to unsubscribe: echo "unsubscribe xml" | mail majordomo@rpmfind.net
#include <stdio.h>
#include "libxml/parser.h"
#include "libxml/tree.h"
#include "libxml/xmlmemory.h"
#include "libxml/xpath.h"
extern int xmlDoValidityCheckingDefaultValue;
int
main(int argc, char **argv)
{
xmlDocPtr doc;
xmlNodePtr root;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr path;
const xmlChar *prop;
if (argc != 2) {
printf("usage:%s <filename>\n", argv[0]);
exit(-1);
}
xmlKeepBlanksDefault(1);
doc = xmlParseFile(argv[1]);
printf("mem used =%d\n", xmlMemUsed());
/* xmlMemDisplay(stdout); */
root = xmlDocGetRootElement(doc);
printf("Document root element name is %s\n", root->name);
/*
* Try to find root element using xpath...
*/
xmlXPathInit();
ctx = xmlXPathNewContext(doc);
path = xmlXPathEvalExpression((const xmlChar *)"id(\"calvin\")", ctx);
printf("Document root element name is %s\n",
path->nodesetval->nodeTab[0]->name);
path = xmlXPathEvalExpression((const xmlChar *)"count(/*/cpu_set_default)", ctx);
printf("Document contains %.0f cpu_set_default elements\n",
path->floatval);
path = xmlXPathEvalExpression((const xmlChar *)
"//cpu_set_default", ctx);
root = path->nodesetval->nodeTab[0];
printf("cpu_set_default Element name is %s\n",
root->name);
printf("Attribute created=%s\n",
prop = xmlGetProp(root, (const xmlChar *)"created"));
free(prop);
xmlSetProp(root, (const xmlChar *)"created", (const xmlChar *)"26");
xmlXPathFreeObject(path);
xmlXPathFreeContext(ctx);
/*
*Dump the document to stdout
*/
/* xmlDocDump(stdout, doc); */
xmlFreeDoc(doc);
/* xmlMemDisplay(stdout); */
printf("mem used =%d\n", xmlMemUsed());
return (0);
}
---- Message from the list xml@rpmfind.net Archived at : http://xmlsoft.org/messages/ to unsubscribe: echo "unsubscribe xml" | mail majordomo@rpmfind.net
This archive was generated by hypermail 2b29 : Fri Feb 16 2001 - 08:44:16 EST