2

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.

1
  • Good question, +1. See my answer for a pure XPath solution -- a one-liner expression that when evaluated produces the wanted value. An explanation is also included. Commented Aug 13, 2011 at 15:00

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.