开发者

Getting certain attribute value in an element with multiple values using xpath PHP

i'm playing around with flickr API lately and having difficulties in parsing th开发者_运维百科eir response to extract the info i need,

here is a sample response:

−
<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 need the get just the source attribute value where the size attribute value is Medium

here is how i'm trying to do that with xpath

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

i tried all kind of xpath query combinations but i cant get to the right query

any ideas on that?


OK so looks like the right combination that made this work is

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

Thanks guys :)


I think you have a mising @ for label because it's an attribute:

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


Missing closing quote after Medium.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜