PHPDOMXPath-如何仅在当前节点中获取图像,而不在子节点中获取图像?

PHPDOMXPath-如何仅在当前节点中获取图像,而不在子节点中获取图像?,php,domxpath,Php,Domxpath,我只需要获取当前节点中的图像,而不需要获取子节点中的图像 我只想要绿色/黄色/红色/黑色图像,而不想要不重要的.gif图像 我可以使用query。//table/tr/td/img' 但我需要它在循环中 <?php ///////////////////////////////////////////////////////////////////// $html=' <table> <tr&

我只需要获取当前节点中的图像,而不需要获取子节点中的图像
我只想要
绿色/黄色/红色/黑色
图像,而不想要
不重要的.gif
图像
我可以使用query
。//table/tr/td/img'

但我需要它在循环中

<?php
    /////////////////////////////////////////////////////////////////////
        $html='
            <table>
                <tr>
                    <td colspan="2">
                        <span>
                            <img src="not_important.gif" />
                        </span>
                        <img src="green.gif" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <span>yellow</span>
                        <img src="yellow.gif" />
                    </td>
                    <td>
                        <span>red</span>
                        <img src="red.gif" />
                    </td>
                </tr>
            </table>
            <table>
                <tr>
                    <td>
                        <span>
                            <img src="not_important.gif" />
                        </span>
                        <img src="black.gif" />
                    </td>
                </tr>
            </table>
        ';
    /////////////////////////////////////////////////////////////////////
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        $xpath = new DomXPath($dom);
    /////////////////////////////////////////////////////////////////////
        $query = $xpath->query('.//table/tr/td');
        for( $x=0,$results=''; $x<$query->length; $x++ )
        {
            $x1=$x+1;

            $image = $query->item($x)->getELementsByTagName('img')->item(0)->getAttribute('src');

            $results .= "image $x1 is : $image<br/>";
        }
        echo $results;
    /////////////////////////////////////////////////////////////////////
?>

我可以通过
$query->item()->

我尝试了
has_属性
getelementsbytagnames
getElementById


但是我失败了:

查询
//table/tr/td/img
应该可以正常工作,因为不需要的图像都位于
元素中

你的循环看起来像

$images = $xpath->query('//table/tr/td/img');
$results = '';
for ($i = 0; $i < $images->length; $i++) {
    $results .= sprintf('image %d is: %s<br />',
                        $i + 1,
                        $images->item($i)->getAttribute('src'));
}
$images=$xpath->query('//table/tr/td/img');
$results='';
对于($i=0;$i<$images->length;$i++){
$results.=sprintf('图像%d是:%s
,', $i+1, $images->item($i)->getAttribute('src'); }
查询
//table/tr/td/img
应该可以正常工作,因为不需要的图像都位于
元素中

你的循环看起来像

$images = $xpath->query('//table/tr/td/img');
$results = '';
for ($i = 0; $i < $images->length; $i++) {
    $results .= sprintf('image %d is: %s<br />',
                        $i + 1,
                        $images->item($i)->getAttribute('src'));
}
$images=$xpath->query('//table/tr/td/img');
$results='';
对于($i=0;$i<$images->length;$i++){
$results.=sprintf('图像%d是:%s
,', $i+1, $images->item($i)->getAttribute('src'); }
更换:

$image = $query->item($x)->getELementsByTagName('img')->item(0)->getAttribute('src');
…与:

$td = $query->item($x); // grab the td element
$img = $xpath->query('./img',$td)->item(0); // grab the first direct img child element
$image = $img->getAttribute('src'); // grab the source of the image
换句话说,再次使用
XPath
对象进行查询,但是现在对于
/img
,相对于您作为
query()
的第二个参数提供的上下文节点。上下文节点是早期结果的元素之一(
td
)。

替换:

$image = $query->item($x)->getELementsByTagName('img')->item(0)->getAttribute('src');
…与:

$td = $query->item($x); // grab the td element
$img = $xpath->query('./img',$td)->item(0); // grab the first direct img child element
$image = $img->getAttribute('src'); // grab the source of the image

换句话说,再次使用
XPath
对象进行查询,但是现在对于
/img
,相对于您作为
query()
的第二个参数提供的上下文节点。上下文节点是早期结果的元素之一(
td
)。

嗨,Phil,是的,我知道,但有时我需要获取图像src和其他节点属性和值,并生成一些条件。像那样的。。所以我需要在循环中使用它。@aldr然后用一些例子相应地更新你的问题,我们不是读心术的人你是对的,但我已经说过我需要在循环+1中使用它:@aldr,我的答案确实包含了一个循环。你必须增加更多关于你真正想要什么的细节。仅供参考,我认为fireeyedboy的答案更接近你想要的Phil,是的,我知道,但有时我需要获取图像src和其他节点属性和值,并制作一些条件。像那样的。。所以我需要在循环中使用它。@aldr然后用一些例子相应地更新你的问题,我们不是读心术的人你是对的,但我已经说过我需要在循环+1中使用它:@aldr,我的答案确实包含了一个循环。你必须增加更多关于你真正想要什么的细节。仅供参考,我认为fireeyedboy的答案更接近你想要的。不清楚为什么@Phil的答案不正确。不清楚为什么@Phil的答案不正确。