开发者

Target xml node using attribute, then return value of different attribute using simpleXML

开发者 https://www.devze.com 2023-03-26 00:57 出处:网络
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:/

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 heigh开发者_StackOverflowt="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.


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.


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.


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


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.

0

精彩评论

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