php DomXPath - 如何从当前节点的父节点中选择子节点?
我有一个非常重要的问题,对我来说似乎很难.. 只是我需要从当前节点的父节点中选择子节点! 我知道我们使用 (..) 来选择父节点。 但就我而言还不够.. 这是我的 php 代码来获取所有颜色值
<?php
/////////////////////////////////////////////////////////////////////
$html='
<table>
<tr>
<td colspan="2">
<span>green</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>black</span>
<img src="black.gif" />
</td>
</tr>
</table>
';
/////////////////////////////////////////////////////////////////////
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
/////////////////////////////////////////////////////////////////////
$evaluate = $xpath->evaluate('.//table/tr/td/span');
for($x=0,$results=''; $x<$evaluate->length; $x++)
{
$x1=$x+1;
$query1 = $xpath->query('.//table/tr/td/span');
$color = $query1->item($x)->nodeValue;
$results .= "color $x1 is : $color<br/>";
}
echo $results;
/////////////////////////////////////////////////////////////////////
?>
* now i need to get for every color its images src ..
i tried this but no way
<?php
/////////////////////////////////////////////////////////////////////
$html='
<table>
<tr>
<td colspan="2">
<span>green</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>black</span>
<img src="black.gif" />
</td>
</tr>
</table>
';
/////////////////////////////////////////////////////////////////////
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
/////////////////////////////////////////////////////////////////////
$evaluate = $xpath->evaluate('.//table/tr/td/span');
for($x=0,$results=''; $x<$evaluate->length; $x++)
{
$x1=$x+1;
$query1 = $xpath->query('.//table/tr/td/span');
$color = $query1->item($x)->nodeValue;
$query2 = $xpath->query('.//table/tr/td/span['.$x1.']/../img');
$image = $query2->item($x)->getAttribute("src");
$results .= "color $x1 is : $color - and- image $x1 is : $image<br/>";
}
echo $results;
/////////////////////////////////////////////////////////////////////
?>
请帮助::
i have a very important question that seem to be difficult for me ..
simply i need to select child node from parent node of current node !!
i know that we use (..) to select parent node .
but in my case it not enough ..
this is my php code to get all color values
<?php
/////////////////////////////////////////////////////////////////////
$html='
<table>
<tr>
<td colspan="2">
<span>green</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>black</span>
<img src="black.gif" />
</td>
</tr>
</table>
';
/////////////////////////////////////////////////////////////////////
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
/////////////////////////////////////////////////////////////////////
$evaluate = $xpath->evaluate('.//table/tr/td/span');
for($x=0,$results=''; $x<$evaluate->length; $x++)
{
$x1=$x+1;
$query1 = $xpath->query('.//table/tr/td/span');
$color = $query1->item($x)->nodeValue;
$results .= "color $x1 is : $color<br/>";
}
echo $results;
/////////////////////////////////////////////////////////////////////
?>
* now i need to get for every color its images src ..
i tried this but no way
<?php
/////////////////////////////////////////////////////////////////////
$html='
<table>
<tr>
<td colspan="2">
<span>green</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>black</span>
<img src="black.gif" />
</td>
</tr>
</table>
';
/////////////////////////////////////////////////////////////////////
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
/////////////////////////////////////////////////////////////////////
$evaluate = $xpath->evaluate('.//table/tr/td/span');
for($x=0,$results=''; $x<$evaluate->length; $x++)
{
$x1=$x+1;
$query1 = $xpath->query('.//table/tr/td/span');
$color = $query1->item($x)->nodeValue;
$query2 = $xpath->query('.//table/tr/td/span['.$x1.']/../img');
$image = $query2->item($x)->getAttribute("src");
$results .= "color $x1 is : $color - and- image $x1 is : $image<br/>";
}
echo $results;
/////////////////////////////////////////////////////////////////////
?>
please help ::
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
没有必要运行完整的 xpath 查询两次。 DOM 树中的每个节点都完全了解其父节点和子节点。
Try this:
There's no need to run full xpath queries twice. Every node in a DOM tree is fully aware of its parents and children.