我使用 php xpath 从下面的 xml feed 和 php 函数中获取值。
<FOUND>
<IMAGES>
<IMAGE>
<SMALL>images/small.jpg</SMALL>
<MED>images/med.jpg</MED>
<LARGE>images/large.jpg</LARGE>
<EXTRAL>images/extra.jpg</EXTRAL>
</IMAGE>
<IMAGE>
<SMALL>images1/small.jpg</SMALL>
<MED>images1/med.jpg </MED>
<LARGE>images1/large.jpg</LARGE>
<EXTRAL>images1/extra.jpg</EXTRAL>
</IMAGE>
<IMAGE>
<SMALL>images2/small.jpg</SMALL>
<MED>images2/med.jpg </MED>
<LARGE>images2/large.jpg</LARGE>
<EXTRAL>images2/extra.jpg</EXTRAL>
</IMAGES>
</FOUND>
function img ($id){
$xml=simplexml_load_file("feed1.xml");
//$xml=simplexml_load_string($string);
if (!$xml) {
trigger_error("There was an error",E_USER_ERROR);
}
$images=$xml->xpath('//IMAGE');
return $images;
}
上面只是部分代码,因为提要非常巨大..我想知道是否有方法只抓取图像对象的第一和第三 block ,跳过第二 block 并显示剩余的图像 block ?
我有一个单独的巨大的 xml feed,它与上面的 feed 非常相似,它像 80 个图像对象 block 。所以我想在每 10 个 block 之后显示一条消息。我怎么做到这一点?
任何帮助将不胜感激
最佳答案
要获得第一个和第三个 block ,您可以执行以下操作:
//IMAGE[position() == 1 || position() == 3]
为了获得例如您可以执行的每 10 个元素:
//IMAGE[position() mod 10 == 0]
我不太清楚每 10 个 block 后显示一条消息到底是什么意思,但你可以这样做:
$images = $xml->xpath('//IMAGE');
foreach($images as $key => $image) {
// do whatever you want to do with the image
if($key % 10 == 9) {
echo 'Some message';
}
}
关于php - 使用 php 跳过 xml feed 中的某些对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2213969/