Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Accessing the attributes of XML elements with namespaces

Reply
Thread Tools

Accessing the attributes of XML elements with namespaces

 
 
matthewjbarr@gmail.com
Guest
Posts: n/a
 
      02-10-2007
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!

***

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      02-10-2007
wrote:
> 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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-11-2007
wrote:


> 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".


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing elements with namespaces johkar XML 3 03-13-2008 03:50 PM
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
Namespaces & prefixed attributes (one more time) kj XML 2 04-23-2004 05:35 PM
accessing documentation elements/unhandled attributes via the som wooks XML 1 01-18-2004 11:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57