|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-08-06 14:22 UTC] frank dot mohaupt at brainworx dot audio
Description:
------------
If the value of node "foo" is empty while adding the node, its output (SimpleXMLElement::asXML()) is "<foo/>".
If the value of node "foo" is overwritten with an empty value ($sxe->foo = ''), its output is "<foo></foo>" instead of "<foo/>".
Test script:
---------------
$sxe = new SimpleXMLElement('<foo></foo>');
$sxe->addChild('bar');
$arrResults['empty node'] = $sxe->asXML();
$sxe = new SimpleXMLElement('<foo></foo>');
$sxe->addChild('bar', '');
$arrResults['empty string'] = $sxe->asXML();
$sxe = new SimpleXMLElement('<foo></foo>');
$sxe->addChild('bar');
$sxe->bar = '';
$arrResults['overwritten empty string'] = $sxe->asXML();
foreach($arrResults as $key => $strResult){
echo $key . ': ' . $strResult;
}
// output
// empty node: <?xml version="1.0"?>\n<foo><bar/></foo>\n
// empty string: <?xml version="1.0"?>\n<foo><bar/></foo>\n
// overwritten empty string: <?xml version="1.0"?>\n<foo><bar></bar></foo>\n
Expected result:
----------------
I expect to get the same result for all three cases.
Patchesworkaround (last revision 2018-08-06 22:08 UTC by cmb@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
The XML spec explicitly says empty elements (no content) can be either a start/end tag pair or a self-closing tag. They are definitely equivalent, though it does later say one "SHOULD" use self-closed tags when the element is defined (like in a spec) to be empty. For the record. For comparison, in DOMNode (a) Setting ->textContent uses xmlNodeSetContent("") + xmlNodeAddContent and produces a self-closed element (b) Setting ->nodeValue uses xmlNodeSetContentLen and produces an element with no content https://3v4l.org/Of5p1 I see SimpleXML is a library on top of libxml, one that goes beyond being a simple abstraction layer that PHP often does, and libraries regularly do things like normalize behavior from the underlying API. Personally I would go ahead and patch it.