title: XPath scraping two nodes values from HTML only if both exist
body: <p>I am using Curl, XPath and PHP in order to scrape product names and prices from HTML source code.  Here is a sample similar to the source code I am examining:</p>
  
  <pre><code>&lt;div class="Gamesdb"&gt;
    &lt;p class="media-title"&gt;
      &lt;a href="/Games/Console/4-/105/Bluetooth-Headset/"&gt;Bluetooth Headset&lt;/a&gt;
    &lt;/p&gt;
    &lt;p class="sub-title"&gt; Console &lt;/p&gt;
    &lt;p class="rating star-50"&gt;
      &lt;a href="/Games/Console/4-/105/Bluetooth-Headset/ProductReviews.html"&gt;(1)&lt;/a&gt;
    &lt;/p&gt;
    &lt;p class="mt5"&gt;
      &lt;span class="price-preffix"&gt;
        &lt;a href="/Games/Console/4-/105/Bluetooth-Headset/"&gt;1 New&lt;/a&gt;
        from 
      &lt;/span&gt;
      &lt;a class="wt-link" href="/Games/Console/4-/105/Bluetooth-Headset/"&gt;
        &lt;span class="price"&gt;
          &lt;em&gt;£34&lt;/em&gt;
          .99
        &lt;/span&gt;
        &lt;span class="free-delivery"&gt; FREE delivery&lt;/span&gt;
      &lt;/a&gt;
    &lt;/p&gt;
    &lt;p class="mt10"&gt;
      &lt;a class="primary button" href="/Games/Console/4-/105/Bluetooth-Headset/"&gt;
        Product Details
        &lt;span style="color: rgb(255, 255, 255); margin-left: 6px; font-size: 16px;"&gt;»&lt;/span&gt;
      &lt;/a&gt;
    &lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I want to extract the media title i.e: </p>
  
  <pre><code>&lt;p class="media-title"&gt;
      &lt;a href="/Games/Console/4-/105/Bluetooth-Headset/"&gt;Bluetooth Headset&lt;/a&gt;
      &lt;/p&gt;
  </code></pre>
  
  <p>Only when the following price class is also present:</p>
  
  <pre><code>&lt;span class="price"&gt;
      &lt;em&gt;£34&lt;/em&gt;
      .99
      &lt;/span&gt;
  </code></pre>
  
  <p>Many of the other products listed don't include it.
  I need to extract both the product name and price or nothing at all and move on to the next product.</p>
  
  <p>Here is a sample of the code i am currently using which is effective at getting all the results regardless of any other conditions:</p>
  
  <pre><code>$results=file_get_contents('SCRAPEDHTML.txt');
  
  $html = new DOMDocument();
  @$html-&gt;loadHtml($results);
  $xpath = new DOMXPath($html);
  $nodelist = $xpath-&gt;query('//p[@class="media-title"]|//span[@class="price"]');
  
  foreach ($nodelist as $n){
  
  $results2[]=$n-&gt;nodeValue;
  
  }
  </code></pre>
  
  <p>I believe this is possible using the correct xpath query but have so far been unable to achieve it. Many thanks in advance.</p>