2016-06-10 53 views
0

我有轻微的不同structure.i页面索引正在通过许多网页试图循环,做到以下几点: -检查不同的节点和相应的呼应

1.如果td[2]具有跨类THN呼应类。 2.if td[2]具有img节点,然后对srchttp://example.com/img/star01.giff的节点数进行计数并回显总数。

我能够做第一部分,但不是第二部分。

pagetype1

<tbody> 

<tr> 
    <td>Name1</td> 
    <td> 
     <span class="star5-05"> 
    </td> 
</tr> 
<tr> 
    <td>Name2</td> 
    <td> 
     <span class="star5-05"> 
    </td> 
</tr> 
</tbody> 

pagetype2

<tbody> 
    <tr> 
     <td>Name1</td> 
     <td> 
      <img alt="" src="http://example.com/img/star01.gif"> 
      <img alt="" src="http://example.com/img/star01.gif"> 
      <img alt="" src="http://example.com/img/star01.gif"> 
      <img alt="" src="http://example.com/img/star01.gif"> 
      <img alt="" src="http://example.com/img/star02.gif"> 
     </td> 
    </tr> 
    <tr> 
     <td>Name2</td> 
     <td> 
      <img alt="" src="http://example.comimg/star01.gif"> 
      <img alt="" src="http://example.comimg/star01.gif"> 
      <img alt="" src="http://example.comimg/star01.gif"> 
      <img alt="" src="http://example.comimg/star01.gif"> 
      <img alt="" src="http://example.com/img/star02.gif"> 
     </td> 
    </tr> 
    </tbody> 

我的代码

foreach($my_nodes as $my_node) 
    { 
    $tmp=$my_xpath->query('td[1]',$my_node); 

    if ($tmp->length>0) 
     { 
     $tmp=$tmp->item(0)->textContent; 

     if ($tmp=="Name1") 
      { 
      $chkstars=$my_xpath->query('td[2]/span/@class',$my_node); 
      if ($chkstars->length>0) 
       { 
       $tmp_stars=$chkstars->item(0)->textContent; 
       } 
       else 
       { 
       $tmp_stars=$my_xpath->evaluate('count(//td[2]/img[@src="http://www.example.com/img/star01.gif"]),$my_node)'); 
       } 
      echo $tmp_stars."<br>"; 
      } 

     } 

    } 
+0

'$ my_xpath->评估('计数(// IMG [@ SRC =“HTTP:/ /example.comimg/star01.gif“]));' – splash58

+0

在哪里我应该把这个计数(// img [@ src =”http://example.comimg/star01.gif“]) –

+0

或者如果你想只计算该行的img - '$ my_xpath-> evaluate('count(.mimg [@ src =“http://example.comimg/star01.gif”]),$ my_node);' – splash58

回答

1

事实上,unswer是Xpath count(.//img[@src="http://example.comimg/star01.gif"])。仅用于测试目的的所有代码

$my_xpath = new DOMXPath($my_doc); 
$my_nodes = $my_xpath->query('//tbody/tr'); 
foreach($my_nodes as $my_node) 
{ 
$tmp=$my_xpath->query('td[2]',$my_node); 
    if ($tmp->length>0) 
    { 
    $tmp=$tmp->item(0)->textContent;  
     if ($chkstars=$my_xpath->evaluate('string(td[2]/span/@class)',$my_node)) 
       echo 'class: ' . $chkstars . "\n"; 
     if($count = $my_xpath->evaluate('count(.//img[@src="http://example.comimg/star01.gif"])',$my‌​_node)) 
       echo 'count: ' . $count . "\n"; 
    } 
} 

demo(更改为STR1或str所不同的XML))

+0

我改变了代码... plz se我的代码...它仍然没有正确回声...我需要它回显tmp_stars如果td [1]是Name1 –

+0

https://eval.in/586688 – splash58