Use not in xpath query

I am getting certain data from a site for which I am using XPath, but for that I need to exclude a few variables for which I must use NOT. but this does NOT work in the code, please explain what I need to do to make it work:

heres 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 get only non td containing class and align, and for that i use 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 offer your opinions

+3


source to share


1 answer


The problem with your logic is that no element matters @class

and @align

will therefore not()

always matter true

.

Instead, you should exclude elements that have either an attribute:

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

      

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

:



//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
}

      

0


source







All Articles