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

在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

图片转代码服务由CSDN问答提供 功能建议

我从我正在使用XPath的站点获取特定数据但为此我必须排除少量变量 我必须使用NOT。 但是这不是代码中没有用的,请解释我要做些什么来使它工作:

继承人的HTML代码 \ n

 &lt; tr&gt;&lt; td colspan =“2”valign =“top”align =“left”&gt;&lt; span class =“tl-document”&gt; 
&lt; left&gt; some text 这里
&lt; / left&gt; 
&lt; / span&gt;&lt; / td&gt;&lt; / tr&gt; 
&lt; tr&gt;&lt; td colspan =“2”valign =“top”align =“left”&gt; 
&lt  ; span class =“text-id”&gt;这里的一些文字,&lt; sup&gt; a&lt; / sup&gt;&lt; sup&gt; b&lt; / sup&gt;&lt; / span&gt; 
&lt; span class =“text-id”&gt  ;这里的一些文字,&lt; sup&gt; a&lt; / sup&gt;&lt; / span&gt; 
&lt; / td&gt;&lt; / tr&gt; 
&lt; tr&gt;&lt; td colspan =“2”valign =“top”class =  “右”&gt; 
&lt; sup&gt; a&lt; / sup&gt;这里的一些文字&lt; br&gt; 
&lt; / td&gt;&lt; / tr&gt; 
&lt; tr&gt;&lt; td colspan =“2”valign =“top”  class =“right”&gt; 
&lt; sup&gt; b&lt; / sup&gt;这里的一些文字&lt; br&gt; 
&lt; / td&gt;&lt; / tr&gt; 
&lt; td colspan =“2”valign =“top”&gt;  
&lt; br&gt;&lt; div&gt; 
&lt; span class =“tl-default”&gt;目标&lt; / span&gt; 
&lt; p&gt;一些t  ext此处,&lt; / p&gt; 
&lt; / div&gt; 
&lt; div&gt; 
&lt; span class =“tl-default”&gt;方法&lt; / span&gt; 
&lt; p&gt;这里的一些文字&lt; / p&gt  ; 
&lt; / div&gt; 
&lt; div&gt; 
&lt; / td&gt; 
&lt; td colspan =“2”valign =“top”&gt; 
&lt; br&gt;&lt; div&gt; 
&lt; span class =“  tl-default“&gt;目标&lt; / span&gt; 
&lt; p&gt;这里有一些文字,&lt; / p&gt; 
&lt; / div&gt; 
&lt; / td&gt; 
   
 \  n 

尝试仅获取不包含类的td并对齐,为此我将此方法用于我的xpath:

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

我是新手,所以请提出您的意见

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
    }
    
    打赏 评论

相关推荐 更多相似问题