Looking at this in more detail...I don't think there's a bug here, or a
patch required.

It is not possible to use SimpleXML to add a child element without a
namespace prefix to a parent element that has a namespace element.
Instead, the child element inherits the namespace prefix of the
parent. So this XML fragment:

<ns1:parent>
<child>abc</child>
</ns1:parent>

is impossible to construct with SimpleXML. The SimpleXML extension
would add the <child> element with the ns1 namespace prefix. This is a
problem for me, as I have an XML schema that specifies that the child
elements must not namespace qualified.

Is this possible in an XML schema? I don't know much about schemas, but
just about the XML language itself, but I would have expected a schema
only cared about what namespaces elements were in, not whether they were
prefixed or not. E.g. I would have expected these to be equivalent:

<ns1:parent xmlns:ns1="whatever">
<child xmlns="">abc</child>
</ns1:parent>

<ns1:parent xmlns:ns1="whatever">
<child>abc</child>
</ns1:parent>

<parent xmlns="whatever">
<child xmlns="">abc</child>
</parent>

<ns1:parent xmlns="whatever" xmlns:ns1="whatever">
<child xmlns="">abc</child>
</ns1:parent>

I would also expect, if the child were to be namespaced, that these are
equivalent:

<ns1:parent xmlns="whatever" xmlns:ns1="whatever">
<child>abc</child>
</ns1:parent>

<ns1:parent xmlns:ns1="whatever">
<ns1:child>abc</ns1:child>
</ns1:parent>

<parent xmlns="whatever">
<child>abc</child>
</parent>

$r = new SimpleXMLElement('<r />');
$c1 = $r->addChild('ns1:child1', NULL, 'urn:myspace1');
$c1->addChild('child2');
echo $r->asXML(), "\n";

This should add child2 in the same namespace as child1, as no namespace
is given explicitly for child2. So I would expect

<r><ns1:child1 xmlns:ns1="urn:myspace1"><ns1:child2 /></ns1:child1></r>

or

<r><child1 xmlns="urn:myspace1"><child2 /></child1></r>

$r = new SimpleXMLElement('<r />');
$r->addChild('Thing1', 100, '');
echo $r->asXML(), "\n";

I would expect

<r><Thing1>100</Thing1></r>

Expected result:
----------------
<?xml version="1.0"?>
<r><ns1:child1 xmlns:ns1="urn:myspace1"><ns1:child2/></ns1:child1></r>

This matches my expectation, too.

<?xml version="1.0"?>
<r><Thing1>100</Thing1></r>

And this.

Actual result:
--------------
<?xml version="1.0"?>
<r><ns1:child1 xmlns:ns1="urn:myspace1"><ns1:child2/></ns1:child1></r>

This matches our expectations.

<?xml version="1.0"?>
<r><Thing1 xmlns="">100</Thing1></r>

This doesn't match, but is equivalent, so acceptable.

The current behavior of specifying a blank namespace, is to add a
literal blank namespace (xmlns=""), which isn't a useful result. No
one would be depending on using a blank namepspace to get xmlns="".

No, consider this code.

$r = new SimpleXMLElement('<r xmlns="urn:myspace1" />');
$r->addChild('Thing1',100,'');
echo $r->asXML(), "\n";

I expect

<r xmlns="urn:myspace1"><Thing1 xmlns="" /></r>

and the xmlns="" is necessary. If this functionality were removed, how
could this be done?

Sure, SimpleXML could perhaps output XML that is slightly more
efficient/less verbose than it currently does, but its output is not
incorrect, and the functionality you want to remove is necessary in
other circumstances.

Ben.




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to