title: libxml2 and XPath traversing children and siblings in ANSI C
body: <p>I have done a fair bit of XML stuff in Perl and now I need to do it in ANDI C for a project. Here's the code I wrote with a snippet of the XML. I have had success to a degree, but am having problems with getting siblings, I am sure it's super easy but I just can't get it. There is two functions, one that simply gets the node set (copied directly from xmlsoft.org). The second function is mine.</p>
  
  <pre><code>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-&gt;nodesetval)){
          xmlXPathFreeObject(result);
                  printf("No result\n");
          return NULL;
      }
  
      return result;
  }
  
      void reader(xmlDocPtr xmlDoc, char *xpath)
  {
  
      xmlXPathObjectPtr xpathresult;
      xmlNodeSetPtr node;
      xmlNodeSetPtr node2;
      xmlChar *title;
  
      int cnt;
  
      // parse feed in memory to xml object
      doc = xmlReadMemory(xmlDoc,strlen(xmlDoc),"noname.xml",NULL,0);
  
      if (!doc) criterr("Error parsing xml document");
  
      // get xpath node set (ttn retrieves the value from the token table)
      xpathresult = getnodeset(doc, ( xmlChar * ) xpath);
  
      if (xpathresult) {
          node = xpathresult-&gt;nodesetval;
          printf("Content-type: text/html\n\n");
  
          for (cnt=0;cnt&lt;node-&gt;nodeNr; cnt++) {
  
              title = xmlNodeListGetString(doc, node-&gt;nodeTab[cnt]-&gt;xmlChildrenNode,1);
  
              printf("%d) title= %s&lt;br/&gt;\n",cnt,title);
              xmlFree(title);         
          }
  
          xmlXPathFreeObject(xpathresult);
          xmlFreeDoc(doc);
          xmlCleanupParser();
  
      } else {
          criterr("Xpath failed");
      }
  
      xmlFreeDoc(doc);
  
      criterr("Success");
  }
  </code></pre>
  
  <p>and the xml snippet</p>
  
  <pre><code>&lt;item&gt;
    &lt;title&gt;this is the title&lt;/title&gt;
    &lt;link&gt;this is the link&lt;/link&gt;
    &lt;description&gt;this is the description&lt;/description&gt;
  &lt;/item&gt;
  </code></pre>
  
  <p>if I use an XPath like <code>//item/title</code> I get all the titles, but what I really want is to get the item and then in the node->nodeNr loop, be able to get the title, link and description easily as I have 100's of 'item' blocks, I'm just not sure how to get the children or siblings of that block easily.</p>