Note that there are some explanatory texts on larger screens.

plurals
  1. POlibxml2 error with namespaces and xpath
    primarykey
    data
    text
    <p>I am pasting some code here that compiles with no warning using gcc file.c -lxml2, assuming that libxml2 is installed in your system. </p> <pre><code>#include &lt;libxml/parser.h&gt; #include &lt;libxml/xpath.h&gt; #include &lt;assert.h&gt; #include &lt;libxml/tree.h&gt; #include &lt;libxml/xpathInternals.h&gt; 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; } if(xmlXPathRegisterNs(context, BAD_CAST "new", BAD_CAST "http://www.example.com/new") != 0) { fprintf(stderr,"Error: unable to register NS with prefix"); return NULL; } result = xmlXPathEvalExpression(xpath, context); xmlXPathFreeContext(context); if (result == NULL) { printf("Error in xmlXPathEvalExpression\n"); return NULL; } if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){ xmlXPathFreeObject(result); printf("No result\n"); return NULL; } return result; } int main(int argc, char **argv) { char *docname; xmlDocPtr doc; xmlChar *xpath = (xmlChar*) "/new:book/section1"; xmlNodeSetPtr nodeset; xmlXPathObjectPtr result; int i; xmlChar *keyword; if (argc &lt;= 1) { printf("Usage: %s docname\n", argv[0]); return(0); } docname = argv[1]; doc = getdoc(docname); result = getnodeset (doc, xpath); if (result) { nodeset = result-&gt;nodesetval; for (i=0; i &lt; nodeset-&gt;nodeNr; i++) { keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1); printf("keyword: %s\n", keyword); xmlFree(keyword); } xmlXPathFreeObject (result); } xmlFreeDoc(doc); xmlCleanupParser(); return (1); } </code></pre> <p>My problem is that I want to parse the following xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;book xmlns="http://www.example.com/new"&gt; &lt;section1&gt;Sec_1&lt;/section1&gt; &lt;section2&gt;Sec_2&lt;/section2&gt; &lt;/book&gt; </code></pre> <p>the book element defines a namespace inside that element. I want to print the value in the xpath /book/section1 and it returns NULL. When I am trying to return the element under a namespace I also get errors, ie /new:book/section1</p> <p>I assume that my code fails because I am not using correctly the namespace prefixes. I run out of time. Could you please help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload