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 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿