PHP5: ext/dom: Namespace & Prefix unexpected behavior
| From: | Vivian Steller | Date: | Tue, 17 Feb 2004 21:16:56 +0000 |
| Subject: | PHP5: ext/dom: Namespace & Prefix unexpected behavior | ||
| Groups: | php.general php.xml.dev | ||
| Request: | Send a blank email to php-general+get-178022@lists.php.net to get a copy of this message | ||
Hello,
checking out the behavior of Namespaces & Prefixes of the new dom
implementation of PHP5 I got some (for me!) unexpected results.
look at the following source code: (plz copy&paste it to your editor ;)
<pre>
<?php
// PHP5: ext/dom: Namespace Tests
function printRootNS($xmlCode) {
$doc = new DomDocument();
$doc->loadXML($xmlCode);
$root = $doc->documentElement;
print(trim(htmlspecialchars($doc->saveXML())) . "\n\t => Namespace of
root-Element: <b>" . $root->namespaceURI . "</b>\n\n");
}
function printRootNSNewPrefix($xmlCode, $newPrefix) {
$doc = new DomDocument();
$doc->loadXML($xmlCode);
$root = $doc->documentElement;
$root->prefix = $newPrefix;
print(trim(htmlspecialchars($doc->saveXML())) . "\n\t => Namespace of
root-Element: <b>" . $root->namespaceURI . "</b>\n\n");
}
printRootNS('<prefix:root xmlns:prefix="somedomain.name"
xmlns:anotherprefix="anotherdomain.name"/>');
print("\nI'm going to change prefix with \"\$root->prefix =
'anotherprefix';\" now... \n\n\n");
printRootNSNewPrefix('<prefix:root xmlns:prefix="somedomain.name"
xmlns:anotherprefix="anotherdomain.name"/>', "anotherprefix");
?>
</pre>
This is the corresponding output:
<?xml version="1.0"?>
<prefix:root xmlns:prefix="somedomain.name"
xmlns:anotherprefix="anotherdomain.name"/>
=> Namespace of root-Element: somedomain.name
I'm going to change prefix with "$root->prefix = 'anotherprefix';"
now...
<?xml version="1.0"?>
<xml:root xmlns:prefix="somedomain.name"
xmlns:anotherprefix="anotherdomain.name"/>
=> Namespace of root-Element: http://www.w3.org/XML/1998/namespace
I thought that setting $root->prefix will set the prefix and the
namespaceUri to the other domain... but it seems to me that the parser
doesn't see the "xmlns:anotherprefix" as a prefix-declaration?
maybe i just misunderstood something, so please correct me if so.
I know, namespaces are quite difficult to implement - it isn't my goal to
annoye you with it!
Best regards,
vivian