根据属性值获取XML元素 [英] Getting XML element based on attribute value

查看:105
本文介绍了根据属性值获取XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿. 我有一个XML文件,只想使用PHP来显示medium大小. XML的部分如下所示:

Hey. I have an XML file and would like to use PHP to display the medium size only. The part of the XML looks like this:

<image size="small">/small23.png</image>
<image size="medium">/medium23.png</image>
<image size="large">/large23.png</image>

我想使用<img src="" />在页面上显示图像,但是我不确定如何仅放置中等图像.在遍历所有其他元素的forloop中,我尝试了以下方法:

I'd like to display the image on the page using <img src="" />, but I'm not sure how to put only the medium image. Within a forloop going through all the other elements, I tried this:

if($file->image->attributes()->size == "medium")
   echo "<img src=$file->image />";

但什么也没画.

谢谢

推荐答案

根据您的示例,我假设您正在使用SimpleXML,因此请尝试以下操作:

Based on your example I assume you're using SimpleXML, so try this:

$mediums = $file->xpath("image[@size='medium']");
if (count($mediums)) {
    echo '<img src="' . (string) $mediums[0] . '" />';
}

这篇关于根据属性值获取XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆