Getting specific value of attribute in element with multiple values โ€‹โ€‹using PHP xpath

I have been playing around with the flickr API lately and having difficulty parsing my answer to extract the information I need,

here is an example answer:

โˆ’
<rsp stat="ok">
โˆ’
<sizes canblog="0" canprint="0" candownload="1">
<size label="Square" width="75" height="75" source="http://farm3.static.flickr.com/2306/1555710063_d2c1e7e7cd_s.jpg" url="http://www.flickr.com/photos/30561311@N00/1555710063/sizes/sq/" media="photo"/>
<size label="Thumbnail" width="100" height="67" source="http://farm3.static.flickr.com/2306/1555710063_d2c1e7e7cd_t.jpg" url="http://www.flickr.com/photos/30561311@N00/1555710063/sizes/t/" media="photo"/>
<size label="Small" width="240" height="160" source="http://farm3.static.flickr.com/2306/1555710063_d2c1e7e7cd_m.jpg" url="http://www.flickr.com/photos/30561311@N00/1555710063/sizes/s/" media="photo"/>
<size label="Medium" width="500" height="333" source="http://farm3.static.flickr.com/2306/1555710063_d2c1e7e7cd.jpg" url="http://www.flickr.com/photos/30561311@N00/1555710063/sizes/m/" media="photo"/>
<size label="Original" width="1280" height="853" source="http://farm3.static.flickr.com/2306/1555710063_e081a6600a_o.jpg" url="http://www.flickr.com/photos/30561311@N00/1555710063/sizes/o/" media="photo"/>
</sizes>
</rsp>

      

now I only need to get the original attribute value where the size attribute value is Medium

this is how i try to do it using xpath

$xml = new SimpleXMLElement($result);
$path = '//size[label="Medium"]@source';
$url  $xml->xpath($path);

      

I have tried all kinds of combinations of xpath queries but I cannot get the correct query

any ideas on this?

+2


source to share


3 answers


OK, this is the correct combination that did the job

'//size[@label="Medium"]/@source'

      



Thanks guys:)

+3


source


I think you have mising @

for label

because he attribute

:



'//size[@label="Medium"]@source'

      

+1


source


There is no final quote after Medium.

0


source







All Articles