title: Nodes return empty with libXML2
body: <p>I've extracted this code (as working) from a project. As I am not knowledgeable about <code>libXML</code> or <code>XML</code> I can't find out the problem to solve. The <code>XML</code> file is here <a href="http://www.meteor.gov.tr/FTPDATA/analiz/sonSOA.xml" rel="nofollow" title="here">link</a></p>
  
  <p>I get output: </p>
  
  <pre><code>Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Bolge"
  Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Peryot"
  Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/İli"
  Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Durum"
  Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Mak"
  Segmentation fault
  </code></pre>
  
  <p>As opposed to just getting </p>
  
  <pre><code>İstanbul
  </code></pre>
  
  <p>in the console output. </p>
  
  <p>Can be compiled with this</p>
  
  <pre><code>gcc -Wall main.c -o out $(pkg-config --cflags --libs glib-2.0 libxml-2.0)
  </code></pre>
  
  <p>The following is working code to be pasted into the main.c</p>
  
  <pre><code>#include &lt;glib-2.0/glib.h&gt;
  
  #include &lt;libxml/tree.h&gt;
  #include &lt;libxml/parser.h&gt;
  #include &lt;libxml/xpath.h&gt;
  #include &lt;libxml/xpathInternals.h&gt;
  
  typedef struct weatherXmlSourceNode {
    char *bolge, *peryot, *ili, *durum;
    int mak;
  } weatherSource;
  weatherSource *wsrc[6];
  
  xmlDocPtr doc;
  
  char *grabNodeFromFile(xmlDocPtr xml_doc, char* nodeName, int item_index) {
    char xpath[64];
    xmlXPathContextPtr xpathCtx; 
    xmlXPathObjectPtr xpathObj; 
    xmlNodeSetPtr nodeset;
  
    sprintf(xpath, "/SOA/sehirler[%d]/%s",item_index,nodeName);
  
    xpathCtx = xmlXPathNewContext(xml_doc);
  
    if(xpathCtx == NULL) {
      fprintf(stderr,"Error: unable to create new XPath context\n");
      return(NULL);
    }
  
    /* Evaluate xpath expression */
    xpathObj = xmlXPathEvalExpression((xmlChar *)xpath, xpathCtx);
    xmlXPathFreeContext(xpathCtx);
    if(xpathObj == NULL) {
      fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath);
      return(NULL);
    }
  
    /* Check if xmlXPathEvalExpression had returned nonempty nodeset*/
    if (xmlXPathNodeSetIsEmpty(xpathObj-&gt;nodesetval)) {
      fprintf(stderr,"Error: empty set as a result of evaluation xpath expression \"%s\"",xpath);
      xmlXPathFreeObject (xpathObj);
      return(NULL);
    }
  
    nodeset = xpathObj-&gt;nodesetval;
  
    //there should be only one node in nodeset. So we return contents of the first node in nodeset
    return (char *)xmlNodeListGetString(xml_doc, nodeset-&gt;nodeTab[0]-&gt;xmlChildrenNode,1);
  }
  
  void fill_entry_from_rss(weatherSource *wsrc, xmlDocPtr filename, int doc_index) {
    /* Fill respective elements */
    wsrc-&gt;bolge = grabNodeFromFile(filename, "Bolge", doc_index);
    wsrc-&gt;peryot = grabNodeFromFile(filename, "Peryot", doc_index);
    wsrc-&gt;ili = grabNodeFromFile(filename, "İli", doc_index);
    wsrc-&gt;durum = grabNodeFromFile(filename, "Durum", doc_index);
    wsrc-&gt;mak = grabNodeFromFile(filename, "Mak", doc_index);
  }
  
  int fill_entry_from_rss_loop() {
    //int doc_item_index = 1;
    /* Load XML documents */
    doc = xmlParseFile("sonSOA.xml");
    if (doc == NULL) {
      fprintf(stderr, "Error: unable to parse file \"sonSOA.xml\"\n");
      return(0);
    }
    /* Allocate memory for weather struct */
    int num = 6;
    while (num &gt; 0) {
      wsrc[(num - 1)] = g_new0(weatherSource, 1);
      num--;
    }
    /* Fill rss entries from their respective fields */
    fill_entry_from_rss(wsrc[0], doc, 0);
    printf("%s\n", wsrc[0]-&gt;ili);
    xmlFreeDoc(doc);
    return 1;
  }
  int main() {
    fill_entry_from_rss_loop();
    return 0;
  }
  </code></pre>