使用属性定位 xml 节点,然后使用 simpleXML 返回不同属性的值

发布于 2024-11-27 23:06:24 字数 1209 浏览 1 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同尘 2024-12-04 23:06:24

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

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

也许您只需要修复您的 XPath。

The pure XPath 1.0 expression to achieve the attributes is:

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

May be you have to fix your XPath only.

同展鸳鸯锦 2024-12-04 23:06:24

我想定位 type="primary" 的图像节点并返回
uri 属性的值。

使用此 XPath 单行表达式

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

这将选择 image 元素的名为 uri 的属性,其 type 的字符串值code> 属性是 "primary",它是 images 元素的子元素,而该元素是 XML 文档中顶部元素的子元素。

要仅获取属性的值,请使用此 XPath 表达式

string(/*/images/image[@type="primary"]/@uri)

请注意:这是纯 XPath 解决方案,可与任何符合 W3C XPath 的引擎一起使用。

I want to target the image node where type="primary" and return the
value for the uri attribute.

Use this XPath one-liner expression:

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

This selects the attribute named uri of the image element the string value of whose type attribute is "primary", and that is a child of an images 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:

string(/*/images/image[@type="primary"]/@uri)

Do note: This is a pure XPath solution and can be used with any W3C XPath - compliant engine.

孤君无依 2024-12-04 23:06:24

这个怎么样:

$xml = new SimpleXMLElement(file_get_contents('feed.xml'));
$theUriArray = $xml->xpath('/release/images/image[@type="primary"]');
$theUri = $theUriArray[0]->attributes()->uri;

echo($theUri);

How about this:

$xml = new SimpleXMLElement(file_get_contents('feed.xml'));
$theUriArray = $xml->xpath('/release/images/image[@type="primary"]');
$theUri = $theUriArray[0]->attributes()->uri;

echo($theUri);
找回味觉 2024-12-04 23:06:24

虽然我是内置 DOMDocument 的忠实粉丝,而不是 SimpleXML,因此不太熟悉 SimpleXML...

我相信 $xml->xpath('/release/images/image[@type ="primary"]') 应该为您提供一个节点列表,而不是单个节点。

在您的情况下,我希望可能的解决方案像

$nodes = $xml->xpath('/release/images/image[@type="primary"]'); // get matching nodes
$node = reset($nodes); // get first item
$uri = $node->attributes()->uri;

既然您特别提到使用 SimpleXML 一样简单,我建议您尝试查看调用 $xml->path(... )
但为了完整起见,这就是我使用 DOMDocument 和 DOMXPath 执行此操作的方式(这将有效、有保证、经过测试等):

$doc = new DOMDocument('1.0', 'utf8');
$doc->loadXML($yourXMLString);

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('/release/images/image[@type="primary"]');

$theNodeYouWant = $nodes->item(0); // the first node matching the query
$uri = $theNodeYouWant->getAttribute('uri');

这似乎有点冗长,但这主要是因为我包含了此初始化。

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

$nodes = $xml->xpath('/release/images/image[@type="primary"]'); // get matching nodes
$node = reset($nodes); // get first item
$uri = $node->attributes()->uri;

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):

$doc = new DOMDocument('1.0', 'utf8');
$doc->loadXML($yourXMLString);

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('/release/images/image[@type="primary"]');

$theNodeYouWant = $nodes->item(0); // the first node matching the query
$uri = $theNodeYouWant->getAttribute('uri');

This seems a bit more verbose, but that's mostly because I included the initialization for this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文