title: libxml2 xpath parsing, doesn't work as expected
body: <p>I decided to use <code>libxml2</code> parser for my qt application and im stuck on <code>xpath</code> expressions. I found an example class and methods, and modified this a bit for my needs. The code </p>
  
  <pre><code>QStringList* LibXml2Reader::XPathParsing(QXmlInputSource input)
  {
      xmlInitParser();
  
      xmlDocPtr doc;
      xmlXPathContextPtr xpathCtx;
      xmlXPathObjectPtr xpathObj;
      QStringList *valList =NULL;
  
      QByteArray arr = input.data().toUtf8();  //convert input data to utf8
      int length = arr.length();
      const char* data = arr.data();
  
      doc = xmlRecoverMemory(data,length); // build a tree, ignoring the errors
      if(doc == NULL) { return NULL;}
  
      xpathCtx = xmlXPathNewContext(doc); 
      if(xpathCtx == NULL)
      {
          xmlFreeDoc(doc);
          xmlCleanupParser();
          return NULL;
      }
  
      xpathObj = xmlXPathEvalExpression(BAD_CAST "//[@class='b-domik__nojs']", xpathCtx); //heres the parsing fails
      if(xpathObj == NULL)
      {
          xmlXPathFreeContext(xpathCtx);
          xmlFreeDoc(doc);
          xmlCleanupParser();
          return NULL;
      }
  
      xmlNodeSetPtr nodes = xpathObj-&gt;nodesetval;
      int size = (nodes) ? nodes-&gt;nodeNr : 0;
      if(size==0)
      {
  
          xmlXPathFreeContext(xpathCtx);
          xmlFreeDoc(doc);
          xmlCleanupParser();
          return NULL;
      }
      valList = new QStringList();
      for (int i = 0; i &lt; size; i++)
      {
          xmlNodePtr current = nodes-&gt;nodeTab[i];
          const char* str = (const char*)current-&gt;content;
          qDebug() &lt;&lt; "name: " &lt;&lt; QString::fromLocal8Bit((const char*)current-&gt;name);
          qDebug() &lt;&lt; "content: " &lt;&lt; QString::fromLocal8Bit((const char*)current-&gt;content) &lt;&lt; "\r\n";
          valList-&gt;append(QString::fromLocal8Bit(str));
      }
  
      xmlXPathFreeObject(xpathObj);
      xmlXPathFreeContext(xpathCtx);
      xmlFreeDoc(doc);
      xmlCleanupParser();
      return valList;
  }
  </code></pre>
  
  <p>As an example im making a request to <a href="https://i.stack.imgur.com/2a7p4.jpg" rel="nofollow noreferrer">http://yandex.ru/</a> and trying to get the node with class <code>b-domik__nojs</code> which is basically one div.   </p>
  
  <pre><code>xpathObj = xmlXPathEvalExpression(BAD_CAST "//[@class='b-domik__nojs']", xpathCtx); //heres the parsing fails
  </code></pre>
  
  <p>the problem is the expression <code>//[@class='b-domik__nojs']</code> doesn't work at all. I checked it in firefox <code>xpath</code> ext., and in opera developer tools <code>xpath</code> ext. in there this expression works perfectly. </p>
  
  <p>I also tried to get other nodes with attributes but for some reason <code>xpath</code> for ANY attribute fails. Is there something wrong in my method? Also when i load a tree using <code>xmlRecover</code>, it gives me a lot of parser errors in debug output.</p>
  
  <hr>
  
  <p>Ok i played a bit with my <code>libxml2</code> function more and used <code>"//*"</code> expression to get all elements in the document, but! It returns me only the elements in the first children node of the body tag.                   <a href="https://i.stack.imgur.com/2a7p4.jpg" rel="nofollow noreferrer">This is the yandex.ru dom tree</a></p>
  
  <p>so basically it gets ALL the elements in the first div <code>"div class="b-line b-line_bar"</code>, but doesnt look for the other elements in other child nodes of the <code>&lt;body&gt;</code> for some reason. </p>
  
  <p>Why can that happen? Maybe <code>xmlParseMemory</code> doesnt build a full tree for some reason? Is there any possible solution to fix this.</p>