473,411 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Accessing the attributes of XML elements with namespaces

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!

***

Feb 10 '07 #1
2 1725
ma**********@gmail.com 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
Feb 10 '07 #2
ma**********@gmail.com 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/
Feb 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wooks | last post by:
I have some information embedded in included schemas which I want to access at run-time for the purposes of contructing a GUI (they will support field labels and tool tips). The options seem to...
1
by: Julius Mong | last post by:
Dear all, I have something like this: <html... > <embed ...> </html> Am I out of luck if I wanted to access the embedded DOM and manipulate its content? Or if I have:
17
by: Colin Cogle | last post by:
------- Line 47, column 8: there is no attribute "id" <DIV id="LeftNavigation" style="position:absolute; left:8px; top:6px; width:200p ------- Line 47, column 31: there is no attribute "style"...
4
by: f_salazar | last post by:
English Version !! Hi !! I have a page with a Form, inside the form I have listbox with 'X' elements. Wend I submit the page, an ASP process inside the page have to read those elements and...
15
by: Christopher Benson-Manica | last post by:
When are named elements written with script accessible to script? <html><head><script type="text/javascript"> function ready() { alert( document.getElementsByName("div").length ); }...
1
by: Mantorok | last post by:
Hi all I'm adding some controls in Page_Load and want to add an attribute: control.Attributes = "test"; However, can I access this attribute from javascript? Thanks Kev
1
by: Peter J. Bismuti | last post by:
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?!...
0
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char...
2
by: lasher168 | last post by:
Hi, I have a problem which I have been on pondering all day and wasting time !!! I have a javascript call which looks like this <td width="50%" class="inputLabel"><span...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.