By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

simplexml and attributes with a namespace in front. How do you getthe attribute?

P: n/a
Here's a bit of xml code that works.

<?php

$string =
"<?xml version=\"1.0\" standalone=\"yes\"?>
<world>
<people xmlns:ss=\"http://crap\">
<person id=\"5\">John Doe</person>
<person id=\"2\">Susie Q. Public</person>
</people>
</world>";

$xml = simplexml_load_string($string);
print "<pre>\n";
print_r($xml);
print "</pre>\n";

foreach($xml->people->person as $p)
foreach($p->attributes() as $a =$b)
{
print "$a =$b<br />\n";
}

// prints out
// id =5
// id =2
?>

Now what if I change the first person to this
<person ss:id=\"5\">John Doe</person>

Anyone know how to get the id?

This is a question that is pertinent to an excel .xml file as
you'll get lines like this
<Cell ss:Index="3">stuff</Cell>

btw: there is a guide on ibm that tells you how to do this with php
and dom. I'm just wondering if there's a way to do it with simplexml.

thx.
Jul 7 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
figured it out
When the attribute has a namespace eg.
<person ss:id=\"5\">John Doe</person>

you provide the namespace to the attributes as an argument
eg.

foreach($xml->people->person as $p)
foreach($p->attributes("http://crap") as $a =$b)
{
print "$a =$b<br />\n";
}

// id =5 is now printed
fido wrote:
Here's a bit of xml code that works.

<?php

$string =
"<?xml version=\"1.0\" standalone=\"yes\"?>
<world>
<people xmlns:ss=\"http://crap\">
<person id=\"5\">John Doe</person>
<person id=\"2\">Susie Q. Public</person>
</people>
</world>";

$xml = simplexml_load_string($string);
print "<pre>\n";
print_r($xml);
print "</pre>\n";

foreach($xml->people->person as $p)
foreach($p->attributes() as $a =$b)
{
print "$a =$b<br />\n";
}

// prints out
// id =5
// id =2
?>

Now what if I change the first person to this
<person ss:id=\"5\">John Doe</person>

Anyone know how to get the id?

This is a question that is pertinent to an excel .xml file as
you'll get lines like this
<Cell ss:Index="3">stuff</Cell>

btw: there is a guide on ibm that tells you how to do this with php and
dom. I'm just wondering if there's a way to do it with simplexml.

thx.
Jul 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.