From 35a201cc8ef0c3f5b2df88d2e528aabee1048348 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 30 Apr 2021 18:47:09 +0200 Subject: Initial/Final commit --- libxml2-2.9.10/doc/tutorial/apd.html | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 libxml2-2.9.10/doc/tutorial/apd.html (limited to 'libxml2-2.9.10/doc/tutorial/apd.html') diff --git a/libxml2-2.9.10/doc/tutorial/apd.html b/libxml2-2.9.10/doc/tutorial/apd.html new file mode 100644 index 0000000..8f9618d --- /dev/null +++ b/libxml2-2.9.10/doc/tutorial/apd.html @@ -0,0 +1,76 @@ +D. Code for XPath Example

D. Code for XPath Example

+

+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+
+xmlDocPtr
+getdoc (char *docname) {
+	xmlDocPtr doc;
+	doc = xmlParseFile(docname);
+	
+	if (doc == NULL ) {
+		fprintf(stderr,"Document not parsed successfully. \n");
+		return NULL;
+	}
+
+	return doc;
+}
+
+xmlXPathObjectPtr
+getnodeset (xmlDocPtr doc, xmlChar *xpath){
+	
+	xmlXPathContextPtr context;
+	xmlXPathObjectPtr result;
+
+	context = xmlXPathNewContext(doc);
+	if (context == NULL) {
+		printf("Error in xmlXPathNewContext\n");
+		return NULL;
+	}
+	result = xmlXPathEvalExpression(xpath, context);
+	xmlXPathFreeContext(context);
+	if (result == NULL) {
+		printf("Error in xmlXPathEvalExpression\n");
+		return NULL;
+	}
+	if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
+		xmlXPathFreeObject(result);
+                printf("No result\n");
+		return NULL;
+	}
+	return result;
+}
+int
+main(int argc, char **argv) {
+
+	char *docname;
+	xmlDocPtr doc;
+	xmlChar *xpath = (xmlChar*) "//keyword";
+	xmlNodeSetPtr nodeset;
+	xmlXPathObjectPtr result;
+	int i;
+	xmlChar *keyword;
+		
+	if (argc <= 1) {
+		printf("Usage: %s docname\n", argv[0]);
+		return(0);
+	}
+
+	docname = argv[1];
+	doc = getdoc(docname);
+	result = getnodeset (doc, xpath);
+	if (result) {
+		nodeset = result->nodesetval;
+		for (i=0; i < nodeset->nodeNr; i++) {
+			keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
+		printf("keyword: %s\n", keyword);
+		xmlFree(keyword);
+		}
+		xmlXPathFreeObject (result);
+	}
+	xmlFreeDoc(doc);
+	xmlCleanupParser();
+	return (1);
+}
+

+

-- cgit v1.2.3