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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Linux权限管理相关操作(求解答)
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表
  • ¥15 DbVisualizer Pro 12.0.7 sql commander光标错位 显示位置与实际不符
  • ¥15 android 打包报错
  • ¥15 关于stm32的问题