views:

55

answers:

2

I am using php xpath to get the values from the below xml feed and php the function.

<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;

    }

above is only a partial code cz the feed is quite huge.. i would like to know is there are way to grab only the first and third block of image objects, skipping the 2nd block and display the remaining image blocks?

i have a separate huge xml feed which is quite similar to the above feed , its like 80 image object blocks .so i would like to display a message after each 10 blocks. how i do this?

any help will be much appreciated

+3  A: 

To get the first and third block you can do:

//IMAGE[position() == 1 || position() == 3]

To get e.g. every 10th element you can do:

//IMAGE[position() mod 10 == 0]

See: XPath function reference.

I am not quite sure what you exactly mean with display a message after each 10 block but you can do this:

$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';
    }  
}
Felix Kling
Mike Sherov
@Mike: Thank you for pointing out.
Felix Kling
actually, if it's after the 10 (and not before the 11th, which is important), you want: if($key%10 == 9)
Mike Sherov
@Mike: Thank you again. I guess I got confused as the elements in XML are indexed starting from `1`.
Felix Kling
Thank you very much
LiveEn
A: 

For more information about XPath syntax, you can visit http://www.w3schools.com/xpath/xpath_syntax.asp .

And... Since xpath() returns an array, maybe you can do more complex opration in a foreach() loop.

XUE Can