title: php xpath: query within a query result
body: <p>I'm trying to parse an html file.</p>
  
  <p>The idea is to fetch the span's with <code>title</code> and <code>desc</code> classes and to fetch their information in each div that has the attribute class='thebest'.</p>
  
  <p>here is my code:</p>
  
  <pre><code>&lt;?php
  
  $example=&lt;&lt;&lt;KFIR
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;div class="a"&gt;moshe1
  &lt;div class="aa"&gt;haim&lt;/div&gt;
   &lt;/div&gt;
   &lt;div class="a"&gt;moshe2&lt;/div&gt;
   &lt;div class="b"&gt;moshe3&lt;/div&gt;
  
  &lt;div class="thebest"&gt;
  &lt;span class="title"&gt;title1&lt;/span&gt;
  &lt;span class="desc"&gt;desc1&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class="thebest"&gt;
  span class="title"&gt;title2&lt;/span&gt;
  &lt;span class="desc"&gt;desc2&lt;/span&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  KFIR;
  
  
  $doc = new DOMDocument();
  @$doc-&gt;loadHTML($example);
  $xpath = new DOMXPath($doc);
  $expression="//div[@class='thebest']";
  $arts = $xpath-&gt;query($expression);
  
  foreach ($arts as $art) {
      $arts2=$xpath-&gt;query("//span[@class='title']",$art);
      echo $arts2-&gt;item(0)-&gt;nodeValue;
      $arts2=$xpath-&gt;query("//span[@class='desc']",$art);
      echo $arts2-&gt;item(0)-&gt;nodeValue;
  }
  echo "done";
  </code></pre>
  
  <p>the expected results are:</p>
  
  <pre><code>title1desc1title2desc2done 
  </code></pre>
  
  <p>the results that I'm receiving are:</p>
  
  <pre><code>title1desc1title1desc1done
  </code></pre>