Doc #61972 [Com]: addchild treats text as a tag

[email protected] ("your dot buddy22 at yahoo dot in") Fri, 21 Oct 2022 07:22:42 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=61972&edit=1

 ID:                 61972
 Comment by:         your dot buddy22 at yahoo dot in
 Reported by:        crashyn at op dot pl
 Summary:            addchild treats text as a tag
 Status:             Analyzed
 Type:               Documentation Problem
 Package:            SimpleXML related
 Operating System:   Windows XP
 PHP Version:        5.4.2
 Block user comment: N
 Private report:     N

 New Comment:

Anyone can comment on a bug related to $tag, because of facing these issue right now.


Previous Comments:
------------------------------------------------------------------------
[2018-08-07 15:20:39] [email protected]

> Something is wrong here.

No, everything works as expected, albeit the behavior is not
documented.  Firstly, I doubt that the tag has ever been removed
as reported originally; almost certainly the tag was simply not
shown by the browser.

Anyhow, SimpleXMLElement::addChild() uses xmlNewChild() under the
hood, and the relevant documentation[1] states:

| If @content is non NULL, a child list containing the TEXTs and
| ENTITY_REFs node will be created. NOTE: @content is supposed to be
| a piece of XML CDATA, so it allows entity references. XML special
| chars must be escaped first by using xmlEncodeEntitiesReentrant(),
| or xmlNewTextChild() should be used.

When SimpleXMLElement::__toString() is called, it uses
xmlNodeListGetString() under the hood, whose documentation[2]
states:

| Build the string equivalent to the text contained in the Node
| list made of TEXTs and ENTITY_REFs

So *known* entity refs are resolved, while unresolvable entity
refs are skipped (&euro; is not predefined).

Note that creating empty child nodes, and setting their value
afterwards via assignment has different results[3], because the
assignment applies xmlEncodeEntitiesReentrant() automatically.

Changing to documentation issue.

[1] <http://www.xmlsoft.org/html/libxml-tree.html#xmlNewChild>
[2] <http://www.xmlsoft.org/html/libxml-tree.html#xmlNodeListGetString>
[3] <https://3v4l.org/JMd3W>

------------------------------------------------------------------------
[2012-05-25 12:04:01] arjen at react dot com

Something is wrong here.
The tag is not removed, it's not encoded anymore. But &entity; are removed.
See  http://3v4l.org/EJGuL

------------------------------------------------------------------------
[2012-05-25 11:54:40] sjon at hortensius dot net

Shouldn't the values passed to xmlNewChild in addChild go through BAD_CAST like 
all other Xml related methods do?

------------------------------------------------------------------------
[2012-05-20 21:54:40] crashyn at op dot pl

<?php
$xml_header = "<?xml version='1.0' encoding='utf-8'?><xml/>";

$xml = new SimpleXMLElement($xml_header);
$xml->addChild("first_string","this is &lt;mystring&gt;");
$xml->addChild("second_string","this is &lt; mystring&gt;");
$xml->asXML("test.xml");
echo "<pre>" . $xml->first_string . "<br />";	// 'this is '
echo $xml->second_string . "</pre>";			// 'this is < mystring>'
?>

------------------------------------------------------------------------
[2012-05-16 20:55:00] riptide dot tempora at opinehub dot com

Can you provide a test script and its actual vs. expected output to show exactly what you mean?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=61972


--
Edit this bug report at https://bugs.php.net/bug.php?id=61972&edit=1