我有一些xml:

<release id="2276808" status="Accepted">
    <images>
         <image height="600" type="primary" uri="http://s.dsimg.com/image/R-2276808-1302966902.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966902.jpeg" width="600"/>
         <image height="600" type="secondary" uri="http://s.dsimg.com/image/R-2276808-1302966912.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966912.jpeg" width="600"/>
         <image height="600" type="secondary" uri="http://s.dsimg.com/image/R-2276808-1302966919.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966919.jpeg" width="600"/><image height="600" type="secondary" uri="http://s.dsimg.com/image/R-2276808-1302966929.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966929.jpeg" width="600"/>
    </images> ...


我正在使用SimpleXML和php 5.3。

我想定位image所在的type="primary"节点,并返回uri属性的值。

我得到的最接近的是:

$xml->xpath('/release/images/image[@type="primary"]')->attributes()->uri;


失败,因为您不能在attribute()之后调用xpath方法。

最佳答案

实现属性的纯XPath 1.0表达式是:

"/release/images/image[@type="primary"]/@uri"


可能您只需要修复XPath。

10-07 19:53
查看更多