dongyimo1293 2014-12-09 07:06
浏览 174
已采纳

在xpath查询中使用not

i am fetching specific data from a site for which i am using XPath but for this i have to exclude few variables for which i have to use NOT. but this NOT is not working in the code please explain what i have to do to make it work :

heres the html code

<tr><td colspan="2" valign="top" align="left"><span class="tl-document">
<left>some text here
</left>
</span></td></tr>
<tr><td colspan="2" valign="top" align="left">
<span class="text-id">some text here,<sup>a</sup><sup>b</sup></span>
<span class="text-id">some text here,<sup>a</sup></span>
</td></tr>
<tr><td colspan="2" valign="top" class="right">
<sup>a</sup>some text here<br>
</td></tr>
<tr><td colspan="2" valign="top" class="right">
<sup>b</sup>some text here<br>
</td></tr>
<td colspan="2" valign="top">
<br><div>
<span class="tl-default">Objective</span>
<p>some text here,</p>
</div>
<div>
<span class="tl-default">Methods</span>
<p>some text here,</p>
</div>
<div>
</td>
<td colspan="2" valign="top">
<br><div>
<span class="tl-default">Objective</span>
<p>some text here,</p>
</div>
</td>

trying to fetch only not td containing class and align and for this i am using this method for my xpath :

$getnew="http://www.example.com/;
$html = new DOMDocument();
@$html->loadHtmlFile($getnew);
$xpath = new DOMXPath( $html );
$y = $xpath->query('//td[@colspan="2" and valign="top" and (not(@class and @align))]');
$ycnt = $y->length;
for ( $idf=6; $idf<$ycnt; $idf++) 
{ if($idf==6){
  echo "<p class='artbox'>".$y->item($idf)->nodeValue."</p>";}
}

i am new to this so please suggest your opinions

  • 写回答

1条回答

  • dongle3217 2014-12-09 07:13
    关注

    The problem with your logic is that no elements have both @class and @align, so the not() will always yield true.

    Instead you should exclude elements that have either attribute:

    //td[@colspan="2" and @valign="top" and not(@class or @align)]
    

    Alternatively, to match elements that only have those two attributes, you can add a count() condition:

    //td[@colspan="2" and @valign="top" and count(@*)=2]
    

    Update

    $query = '//td[@colspan="2" and @valign="top" and not(@class or @align)]';
    foreach ($xpath->query($query) as $node) {
        // do something with $node
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?