php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55264 Example does nothing
Submitted: 2011-07-22 04:39 UTC Modified: 2011-07-27 20:31 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: jeremybotto at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: php
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jeremybotto at gmail dot com
New email:
PHP Version: OS:

 

 [2011-07-22 04:39 UTC] jeremybotto at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/simplexmlelement.addattribute%23Examples
---

The sample code depends upon the inclusion of example code, example.php, from an 
earlier page.  Without example.php, the sample code does not work.  Running the 
sample code outputs the the input.  There is no visible change on inspection of 
the files, directories, and source code.  I tried stripping parts of example.php 
out, to see if the code sample actually added any attributes, and the sample code 
just throws errors when lines are removed.  Basically, the code does nothing to 
'manipulate' XML.  It simply verifies that a translation of the input matches the 
input.

Can we get an example that doesn't depend upon external files and that actually 
changes something, please?

Expected result:
----------------
Add an attribute to an XML file via PHP.

Actual result:
--------------
Does nothing.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-22 04:52 UTC] salathe@php.net
-Status: Open +Status: Feedback
 [2011-07-22 04:52 UTC] salathe@php.net
Can you clarify a few things, please? 

When you say "there is no visible change", where did you expect anything to be 
changed (the example should print the manipulated XML to the screen, terminal, 
wherever)? What do you mean by "verifies that a translation of the input matches 
the input", there is no translation of any kind going on.

For what it's worth, here's an include-less version of that example showing the 
expected output: http://codepad.org/gwx8oJpw

Finally, with regards to the SimpleXML examples requiring the example.php file, 
this was done to avoid clogging up the examples with 28 lines of code again and 
again.  Requiring external files is not something we usually ask for examples, 
which should be able to run "standalone" ideally.
 [2011-07-27 12:31 UTC] jeremybotto at gmail dot com
That doesn't help at all.  The output is in php, even though I use asXML();, and 
it doesn't change the file.  It just changes the source code of the page.  All 
of the examples change one file and output to the same file, and that doesn't 
leave any room for version control.  Also, at 
http://www.php.net/manual/en/simplexml.examples-basic.php, if you look at the 
examples, they all output in plain text for this same reason.  This doesn't say 
anything about how the nesting is manipulated by SimpleXML, which is crucial to 
understanding SimpleXML and makes the manual impossible to understand.  Look at 
a specific example:

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->xpath('//character') as $character) {
    echo $character->name, 'played by ', $character->actor, PHP_EOL;
}
?>
'//' serves as a wildcard. To specify absolute paths, omit one of the slashes.
The above example will output:
Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr

Fortunately for us, they've followed the example of php.net and not included the 
XML file, so we can't tell what they're manipulating, but it looks like this: 
<characters><character><name>text</name><actor>text</actor></character>.  The 
output doesn't include the element nodes, so there's no way to tell if they have 
been stripped, if new ones have been added, or just what.  Because the document 
doesn't actually come across as XML, it appears that SimpleXML acts to convert 
XML into plain text, but everyone knows that isn't its intended use.  It's 
supposed to allow people to manipulate XML.  So, it should give XML output from 
XML input unless otherwise specified, rather than giving raw text.
 [2011-07-27 13:47 UTC] salathe@php.net
Your comments make no sense in the context of a documentation bug report. If you 
feel something is missing from the documentation, or something documented is 
incorrect, please outline what you want to see changed.
 [2011-07-27 16:42 UTC] jeremybotto at gmail dot com
-Status: Feedback +Status: Open
 [2011-07-27 16:42 UTC] jeremybotto at gmail dot com
The recommendations I receive from PHP developers is to not use SimpleXML because it is too difficult and complicated.  Instead, use domdocument, which is also complicated.  Also, don't use the php manual to learn it.  I'm sure that you are already well aware of the flaws in the documentation.  I seriously doubt anyone is going to take action.

However, I'll entertain you, against my will and better judgment:

1) Include xml files, not php files, in the examples.
2) Use all of the parameters and switches in at least one example.
3) Use xml for the output unless another output format is specified, in which case, use xml output and the specified output where applicable.
 [2011-07-27 17:04 UTC] mfonda@php.net
All pages including 'example.php' note that it is a file containing an XML string, and that it can be found on the basic usage 
page, and they link to the basic usage page.

I think you're misunderstanding how SimpleXMLElement::addAttribute works. In the description, it states "Adds an attribute to 
the SimpleXML element." addAttribute doesn't modify any file, it modifies the SimpleXMLElement object. If you wish to output 
the XML represented by a SimpleXMLElement object, you can use the SimpleXMLElement::asXML() method, as is done in the examples. 
If you look at the XML output in the example versus the original string in example.php, you'll see that attributes were indeed 
added. If you want to write to a file, you could do something like file_put_contents('my_xml_file.xml', $sxe->asXML());
 [2011-07-27 20:31 UTC] salathe@php.net
-Status: Open +Status: Bogus
 [2011-07-27 20:31 UTC] salathe@php.net
Thank you for getting down to some numbered points, Jeremy. Bug reports are not 
the place at all for general rambling comments; they're for reporting 
deficiencies to get fixed.

Point number 2 (Use all of the parameters and switches in at least one example.) 
should be mostly covered in the documentation pages covering those parameters. 
If you find a page without a sufficient example then please do create a new, 
concise, report for each separate defect.  

I would also encourage you, should you wish, to take steps towards rectifying 
this sort of thing yourself by submitting a patch in the Online Documentation 
Editor (https://edit.php.net) which allows anyone to make changes to the 
documentation sources.

As for this particular bug report, the initial comment was that the example for 
SimpleXMLElement::addAttribute() was expected, and failed, to add an attribute 
to an XML file. This is not the expected result of that example so the report 
will be marked as "bogus".

P.S. mfonda, asXML() handles writing to files too, as documented, $sxe-
>asXML("my_xml_file.xml")
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 12:00:02 2026 UTC