title: php xpath table parsing question
body: <p>I have a several tables nested within a table that I am parsing using php xpath.</p>
  
  <p>I'm using a series of xpaths because I'm breaking up the code into conceptual units across several methods calls, and this structure has been working perfectly in other scenarios without nested tables.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>// create a host DOM document
  $dom = new DOMDocument();
  
  // load the html string into the dom
  $dom-&gt;loadHTML($html_string);
  
  // make an xpath object out of the dom
  $xpath = new DOMXpath($dom);
  
  // run query to extract the rows from the master table
  $context_nodes = $xpath-&gt;query('//table[@id="id1"]/tr[position()&gt;1]');
  
  // parse data from the individual tables nested in each master table row
  foreach($context_nodes as $context_node){
      $interesting_nodes[] = $xpath-&gt;query('table[2]/tr[td[2]]', $context_node);
  }
  </code></pre>
  
  <p>The resulting $interesting_nodes array contains empty DOMNodeLists.</p>
  
  <p>The $context_nodes DOMNodeList contains valid data. The html content of each $context_node looks like this:</p>
  
  <pre><code>&lt;td&gt;
      &lt;table&gt;&lt;/table&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>I tried the following simplified $intesting_nodes query to match any table:</p>
  
  <pre><code>$intesting_nodes[] = $xpath-&gt;query('table', $context_node);
  </code></pre>
  
  <p>But that still produces the same empty DOMNodeLists.</p>
  
  <p><strong>And now the interesting part</strong></p>
  
  <p>When I try an $interesting_nodes query like so:</p>
  
  <pre><code>$interesting_nodes[] = $xpath-&gt;query('*[2]/*[*[2]]', $context_node);
  </code></pre>
  
  <p>Then everything works <em>perfectly</em>; but if I replace <em>any</em> "*" with the corresponding "table", "tr", or "td" tags, then the query breaks once again.</p>
  
  <p>Does anyone else have experience with this behavior and relative xpath queries in php?</p>
  
  <p>I would very much like to be able to use a more exact query, and would prefer to be able to keep the query relative like it is rather than making it absolute.</p>