473,326 Members | 2,048 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,326 software developers and data experts.

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

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
1 2647
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
9
by: Soren Kuula | last post by:
Hi, I just can't find namespaces of attributes stated clearly enough in the XML namespace spec. But .. I hear rumors that attributes, unless qualified otherwise, default to the namespace of the...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
2
by: VanOrton | last post by:
Hi all, When XercesDOMParser parses an XML document with a Schema instance, by default it adds all missing default and fixed attributes. I have the impression though that it misses the...
1
by: root | last post by:
Hi folks - hope someone can help me. Firstly, my apologies for crossposting this to alt.php (and not properly crossposting either). I have an XML file with some elements like those below ...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
1
by: WebCM | last post by:
There are lots of problems with XHTML parsing in PHP by XML functions. I've just solved most of them. However, how to get value of attribute with namespace? <input type="checkbox" id="something"...
7
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
1
by: duane.lortie | last post by:
I'm writing a routine that fetches XML and attempts to parse some values from it. A condensed entry node of the XML looks like this .. <entry> <title>Some title</title> <author>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.