Accessing the attributes of XML elements with namespaces

M

matthewjbarr

Sorry for cross-posting, but I posted this in the PHP group and think
it probably should have been here - think maybe I should be looking at
XPath expressions to solve the problem. E.g. something like this:

$item->xpath("//movie:image@src")

***

Hi there,

I'm using PHP5's SimpleXML and the simplexml_load_string() method to
read some XML files.

I can loop round all the elements and extract data from the elements
using the usual syntax e.g.

foreach ($xml->list->item as $myitem) { print($myitem->title); }

However, I don't know how to access the value of an attribute where
the element has a namespace associated with it, e.g.

<item>
<movie:title>Star Wars</movie:title>
<movie:image src="anImage.jpg" alt="Star Wars" />
</item>

How do I get the value of the src attribute, i.e. "anImage.jpg"?

Thanks!

***
 
J

Joe Kesselman

However, I don't know how to access the value of an attribute where
the element has a namespace associated with it

In XPath 1.0, the normal solution is to use a prefix in the path -- then
use a separate (and unfortunately implementation-specific!) mechanism to
tell the Xpath interpreter what namespace is associated with that path.

(If the XPath is within an XSLT stylesheet, the mechanism is simply to
make sure this prefix is defined at that point in the stylesheet. If
you're just using standalone XPath, check the specs for your XPath engine.)

If you need a portable namespace-aware XPath, there is a kluge-around
that involves matching on * and then using a predicate to check both the
localname and the namespace URI. Ugly, but sometimes unavoidable.



I don't do PHP, so I can't advise you on what the structure-like syntax
would be to access a namespaced node. There probably is one; Read The
Fine Manual... Of course the standard DOM API, which is what most
languages use for this purpose, handles namespaces with no problem.
 
M

Martin Honnen

However, I don't know how to access the value of an attribute where
the element has a namespace associated with it, e.g.

<item>
<movie:title>Star Wars</movie:title>
<movie:image src="anImage.jpg" alt="Star Wars" />
</item>

How do I get the value of the src attribute, i.e. "anImage.jpg"?

Here is an example, tested with PHP 5.2

$xml = <<<END
<item xmlns:movie="http://example.com/2007/movie">
<movie:title>Star Wars</movie:title>
<movie:image src="anImage.jpg" alt="Star Wars" />
</item>
END;

header('Content-Type: text/plain');

$item = new SimpleXMLElement($xml);

$item->registerXPathNamespace('mv', 'http://example.com/2007/movie');

foreach ($item->xpath('mv:image') as $image) {
echo $image['src'] . "\r\n";
}

outputs "anImage.jpg".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top