title: Filtering within DOM XPath query results
body: <p>So I'm using XPath to search a rather large XML document. What I'd like to do is after getting the initial results, search again within those results (and then search up to 2 more times). However, I'm not sure how to go about searching again. I tried this:</p>
  
  <p><strong>Multiple queries using '|' operator:</strong></p>
  
  <pre><code>    $dom = new DOMDocument();
      $dom-&gt;load('courses.kml');
      $xpath = new DOMXPath($dom);
      $xpath-&gt;registerNamespace('kml', "http://earth.google.com/kml/2.1");
      //merge queries using | operator 
  $query = $xpath-&gt;query("//kml:Placemark[kml:type='".$_POST['type']."'] | //kml:Placemark[kml:club_type='".$_POST['club_type']."']"); 
  
              foreach($query as $result){
                   echo $result-&gt;nodeValue . "&lt;br /&gt;&lt;br /&gt;";
              }
  </code></pre>
  
  <p><strong>Apply a 2nd XPath expression to first query object:</strong></p>
  
  <pre><code>$dom = new DOMDocument();
  $dom-&gt;load('courses.kml');
  $xpath = new DOMXPath($dom);
  $xpath-&gt;registerNamespace('kml', "http://earth.google.com/kml/2.1");
  $query = $xpath-&gt;query("//kml:Placemark[kml:type='".$_POST['type']."']");
  
  $xpath2 = new DOMXPath($query);
  $query2 = $xpath2-&gt;query("//[club_type='".$_POST['club_type']."']");
  
          //echo $result-&gt;nodeValue . "&lt;br /&gt;&lt;br /&gt;";
          foreach($query2 as $result2){
               echo $result2-&gt;nodeValue . "&lt;br /&gt;&lt;br /&gt;";
          }
  </code></pre>
  
  <p>But that doesn't seem to work, so I tried:</p>
  
  <p><strong>Run two separate instances and merge</strong> </p>
  
  <pre><code>//1st
  $dom = new DOMDocument();
  $dom-&gt;load('courses.kml');
  $xpath = new DOMXPath($dom);
  $xpath-&gt;registerNamespace('kml', "http://earth.google.com/kml/2.1");
  $query = $xpath-&gt;query("//kml:Placemark[kml:club_type='".$_POST['club_type']."']");
  
  //2nd
  $dom2 = new DOMDocument();
  $dom2-&gt;load('courses.kml');
  $xpath2 = new DOMXPath($dom2);
  $xpath2-&gt;registerNamespace('kml', "http://earth.google.com/kml/2.1");
  $query2 = $xpath2-&gt;query("//kml:Placemark[kml:type='".$_POST['type']."']");
  
  //merge object
  $obj_merged = (array) array_merge((array) $query, (array) $query2);
  foreach($obj_merged as $result){
      echo $result-&gt;nodeValue . "&lt;br /&gt;&lt;br /&gt;";
  }
  </code></pre>
  
  <p>And I've tried various other things including following the suggestions at: <a href="https://stackoverflow.com/questions/3188896">php xpath: query within a query result</a>, though it's not exactly the same thing, I still couldn't produce results. I'm not getting any errors, just blank pages.</p>
  
  <h3>To clarify:</h3>
  
  <p>If this were my XML document:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;kml xmlns="http://earth.google.com/kml/2.1"&gt;
      &lt;Document&gt;
        &lt;Placemark id="placemark1"&gt;
            &lt;name&gt;Test Country Club1 &lt;/name&gt;
            &lt;description&gt;
                &lt;![CDATA[
                   &lt;div class="contact"&gt;Post Office Box 329 &lt;a href="#" target="_blank"&gt;website&lt;/a&gt;&lt;/div&gt;
                ]]&gt;
            &lt;/description&gt;
            &lt;alpha&gt;a&lt;/alpha&gt;
            &lt;position&gt;2&lt;/position&gt;
            &lt;type&gt;Public/Daily Fee&lt;/type&gt;
            &lt;club_type&gt;other&lt;/club_type&gt;
            &lt;hole_type&gt;9hole&lt;/hole_type&gt;
            &lt;styleUrl&gt;#nineholeStyle&lt;/styleUrl&gt;
            &lt;Point&gt;
                &lt;coordinates&gt;-79.285576,37.111809&lt;/coordinates&gt;
            &lt;/Point&gt;
        &lt;/Placemark&gt;
        &lt;Placemark id="placemark2"&gt;
            &lt;name&gt;Test Country Club2&lt;/name&gt;
            &lt;description&gt;
                &lt;![CDATA[
                   &lt;div class="contact"&gt;Post Office Box 329 &lt;a href="#" target="_blank"&gt;website&lt;/a&gt;&lt;/div&gt;
                ]]&gt;
            &lt;/description&gt;
            &lt;alpha&gt;a&lt;/alpha&gt;
            &lt;position&gt;2&lt;/position&gt;
            &lt;type&gt;Public/Daily Fee&lt;/type&gt;
            &lt;club_type&gt;other&lt;/club_type&gt;
            &lt;hole_type&gt;9hole&lt;/hole_type&gt;
            &lt;styleUrl&gt;#nineholeStyle&lt;/styleUrl&gt;
            &lt;Point&gt;
                &lt;coordinates&gt;-79.285576,37.111809&lt;/coordinates&gt;
            &lt;/Point&gt;
        &lt;/Placemark&gt;
        &lt;Placemark id="placemark3"&gt;
            &lt;name&gt;Test Country Club3&lt;/name&gt;
            &lt;description&gt;
                &lt;![CDATA[
                   &lt;div class="contact"&gt;Post Office Box 329 &lt;a href="#" target="_blank"&gt;website&lt;/a&gt;&lt;/div&gt;
                ]]&gt;
            &lt;/description&gt;
            &lt;alpha&gt;a&lt;/alpha&gt;
            &lt;position&gt;3&lt;/position&gt;
            &lt;type&gt;Public/Daily Fee&lt;/type&gt;
            &lt;club_type&gt;other&lt;/club_type&gt;
            &lt;hole_type&gt;9hole&lt;/hole_type&gt;
            &lt;styleUrl&gt;#nineholeStyle&lt;/styleUrl&gt;
            &lt;Point&gt;
                &lt;coordinates&gt;-79.285576,37.111809&lt;/coordinates&gt;
            &lt;/Point&gt;
        &lt;/Placemark&gt;
        &lt;Placemark id="placemark4"&gt;
            &lt;name&gt;Test Country Club4&lt;/name&gt;
            &lt;description&gt;
                &lt;![CDATA[
                   &lt;div class="contact"&gt;Post Office Box 329 &lt;a href="#" target="_blank"&gt;website&lt;/a&gt;&lt;/div&gt;
                ]]&gt;
            &lt;/description&gt;
            &lt;alpha&gt;a&lt;/alpha&gt;
            &lt;position&gt;4&lt;/position&gt;
            &lt;type&gt;Private&lt;/type&gt;
            &lt;club_type&gt;Greengrass&lt;/club_type&gt;
            &lt;hole_type&gt;18hole&lt;/hole_type&gt;
            &lt;styleUrl&gt;#nineholeStyle&lt;/styleUrl&gt;
            &lt;Point&gt;
                &lt;coordinates&gt;-79.285576,37.111809&lt;/coordinates&gt;
            &lt;/Point&gt;
        &lt;/Placemark&gt;
    &lt;/Document&gt;
  &lt;/kml&gt;
  </code></pre>
  
  <p>How would i search for type, club_type, hole_type, and/or alpha. Say if i wanted to only return Private Greengrass 18hole clubs that start with 'a'</p>