|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-11-09 02:48 UTC] jevon at jevon dot org
Description:
------------
Namespace support in SimpleXML is all screwy. In particular, you cannot add two differently-namespaced attributes to a SimpleXML node; the first one is lost.
Reproduce code:
---------------
$xml = new SimpleXMLElement('<root />');
$n = $xml->addChild("node", "value"); // still fails even if we set a NS here
$n->addAttribute("a", "b"); // still fails even if we set a different NS here
$n->addAttribute("c", "d", "http://bar.com");
print_r($xml->asXml());
Expected result:
----------------
<root xmlns:a="http://bar.com"><node a="b" a:c="d">value</node></root>
Actual result:
--------------
<root><node xmlns="http://bar.com" a="b" c="d">value</node></root>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 10:00:01 2025 UTC |
It works just fine if you define your namespace beforehand. $xml = new SimpleXMLElement('<root xmlns:bar="http://bar.com"/>'); $n = $xml->addChild("node", "value"); $n->addAttribute("a", "b"); $n->addAttribute("c", "d", "http://bar.com"); print_r($xml->asXml()); I don't know, perhaps SimpleXML should raise a E_NOTICE on unknown namespace, but it's really a matter of good coding practice. Btw, jevon, I don't recommend saying that something is "screwy" when filing bug reports, it doesn't really motivate developpers of said software to look into them ^^;