title: Extracting information via XPath in PHP
body: <p>Just trying to extract some information from the AEC website (e.g. <a href="http://apps.aec.gov.au/eSearch/LocalitySearchResults.aspx?filter=3977&amp;filterby=Postcode" rel="nofollow">http://apps.aec.gov.au/eSearch/LocalitySearchResults.aspx?filter=3977&amp;filterby=Postcode</a>). The XPath query I'm running is "<code>//x:tbody/x:tr/x:td[4]/x:a</code>", which I've tested in XPath Checker (the Firefox extension) and it pulls up the relevant locality data. </p>
  
  <p>I'm then using PHP to load the page, execute the query and then iterate through the results.</p>
  
  <pre><code>$ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $html = curl_exec($ch);
  curl_close($ch);
  
  # Create a DOM parser object
  $dom = new DOMDocument();
  libxml_use_internal_errors(true);
  
  
   $dom-&gt;loadHTML($html);
  
  $xpath = new DOMXpath($dom);
  
  $elements = $xpath-&gt;query( '//tbody/tr/td[4]/a');
  
  
  foreach ($elements as $element) {
       echo $element;
  }
  </code></pre>
  
  <p>I'm then getting:</p>
  
  <pre><code>Warning: Invalid argument supplied for foreach() in /home/givesh5/public_html/dig/electoratesearch.php on line 41
  </code></pre>
  
  <p>It seems that the query is returning some sort of boolean rather than a list of matches for the query?</p>
  
  <p>Relevant markup as follows:</p>
  
  <pre><code>&lt;table cellspacing="0" rules="all" border="1" id="ContentPlaceHolderBody_gridViewLocalities" style="border-collapse:collapse;"&gt;
          &lt;tr class="headingLink"&gt;
              &lt;th scope="col"&gt;&lt;a href="javascript:__doPostBack(&amp;#39;ctl00$ContentPlaceHolderBody$gridViewLocalities&amp;#39;,&amp;#39;Sort$StateAb&amp;#39;)"&gt;State&lt;/a&gt;&lt;/th&gt;&lt;th scope="col"&gt;&lt;a href="javascript:__doPostBack(&amp;#39;ctl00$ContentPlaceHolderBody$gridViewLocalities&amp;#39;,&amp;#39;Sort$LocalityNm&amp;#39;)"&gt;Locality/Suburb&lt;/a&gt;&lt;/th&gt;&lt;th scope="col"&gt;&lt;a href="javascript:__doPostBack(&amp;#39;ctl00$ContentPlaceHolderBody$gridViewLocalities&amp;#39;,&amp;#39;Sort$Postcode&amp;#39;)"&gt;Postcode&lt;/a&gt;&lt;/th&gt;&lt;th scope="col"&gt;&lt;a href="javascript:__doPostBack(&amp;#39;ctl00$ContentPlaceHolderBody$gridViewLocalities&amp;#39;,&amp;#39;Sort$DivisionNm&amp;#39;)"&gt;Electorate&lt;/a&gt;&lt;/th&gt;&lt;th scope="col"&gt;&lt;a href="javascript:__doPostBack(&amp;#39;ctl00$ContentPlaceHolderBody$gridViewLocalities&amp;#39;,&amp;#39;Sort$DivisionNmRedistributed&amp;#39;)"&gt;Redistributed Electorate&lt;/a&gt;&lt;/th&gt;&lt;th scope="col"&gt;Other Locality(s)&lt;/th&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;BOTANIC RIDGE&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CANNONS CREEK&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CRANBOURNE&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Holt&amp;amp;filterby=Electorate&amp;amp;divid=216"&gt;Holt&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CRANBOURNE EAST&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CRANBOURNE EAST&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Holt&amp;amp;filterby=Electorate&amp;amp;divid=216"&gt;Holt&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CRANBOURNE NORTH&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Holt&amp;amp;filterby=Electorate&amp;amp;divid=216"&gt;Holt&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CRANBOURNE SOUTH&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;CRANBOURNE WEST&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Holt&amp;amp;filterby=Electorate&amp;amp;divid=216"&gt;Holt&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;DEVON MEADOWS&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;FIVEWAYS&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=DEVON+MEADOWS&amp;amp;filterby=LocalityorSuburb&amp;amp;state=VIC"&gt;DEVON MEADOWS&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;JUNCTION VILLAGE&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Flinders&amp;amp;filterby=Electorate&amp;amp;divid=211"&gt;Flinders&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;SANDHURST&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Isaacs&amp;amp;filterby=Electorate&amp;amp;divid=219"&gt;Isaacs&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;SKYE&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Dunkley&amp;amp;filterby=Electorate&amp;amp;divid=210"&gt;Dunkley&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;&lt;tr&gt;
              &lt;td&gt;VIC&lt;/td&gt;&lt;td&gt;SKYE&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=3977&amp;amp;filterby=Postcode"&gt;3977&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="LocalitySearchResults.aspx?filter=Isaacs&amp;amp;filterby=Electorate&amp;amp;divid=219"&gt;Isaacs&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  </code></pre>