使用属性定位 xml 节点,然后使用 simpleXML 返回不同属性的值
我有一些 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。
我想定位 type="primary"
所在的 image
节点并返回 uri 属性的值。
我得到的最接近的是:
$xml->xpath('/release/images/image[@type="primary"]')->attributes()->uri;
它失败了,因为您无法在 xpath
之后调用 attribute()
方法。
I have some 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> ...
I'm using SimpleXML and php 5.3.
I want to target the image
node where type="primary"
and return the value for the uri attribute.
The closest I've gotten is:
$xml->xpath('/release/images/image[@type="primary"]')->attributes()->uri;
which fails because you cannot call attribute()
method after xpath
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实现这些属性的纯 XPath 1.0 表达式是:
也许您只需要修复您的 XPath。
The pure XPath 1.0 expression to achieve the attributes is:
May be you have to fix your XPath only.
使用此 XPath 单行表达式:
这将选择
image
元素的名为uri
的属性,其type
的字符串值code> 属性是"primary"
,它是images
元素的子元素,而该元素是 XML 文档中顶部元素的子元素。要仅获取属性的值,请使用此 XPath 表达式:
请注意:这是纯 XPath 解决方案,可与任何符合 W3C XPath 的引擎一起使用。
Use this XPath one-liner expression:
This selects the attribute named
uri
of theimage
element the string value of whosetype
attribute is"primary"
, and that is a child of animages
element` that is a child of the top element in the XML document.To get just the value of the attribute, use this XPath expression:
Do note: This is a pure XPath solution and can be used with any W3C XPath - compliant engine.
这个怎么样:
How about this:
虽然我是内置 DOMDocument 的忠实粉丝,而不是 SimpleXML,因此不太熟悉 SimpleXML...
我相信
$xml->xpath('/release/images/image[@type ="primary"]')
应该为您提供一个节点列表,而不是单个节点。在您的情况下,我希望可能的解决方案像
既然您特别提到使用 SimpleXML 一样简单,我建议您尝试查看调用
$xml->path(... )
但为了完整起见,这就是我使用 DOMDocument 和 DOMXPath 执行此操作的方式(这将有效、有保证、经过测试等):
这似乎有点冗长,但这主要是因为我包含了此初始化。
While I'm a big fan of the built-in DOMDocument as opposed to SimpleXML and therefor not all that familiar with SimpleXML...
I believe
$xml->xpath('/release/images/image[@type="primary"]')
Should give you a list of nodes, not a single node.In your case, I'd expect a possible solution to be as simple as
Since you specifically mention using SimpleXML, I'd suggest you try looking at the result of your call to
$xml->path(...)
But for completeness, this is how I'd do it using DOMDocument and DOMXPath (which will work, guaranteed, tested and all):
This seems a bit more verbose, but that's mostly because I included the initialization for this one.