|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37669 Setting a node's value from array returned by xpath does not work
Submitted: 2006-06-02 00:44 UTC Modified: 2006-12-30 13:42 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: Assigned: helly (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: 5.1.4 OS: Linux (Gentoo, RHEL)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2006-06-02 00:44 UTC]
When trying to set the value (inside text) of a node from an array returned from an xpath call nothing happens. Setting an attribute works fine, however. Setting via normal -> access works as expected.

Reproduce code:
$xml = simplexml_load_string('<root><node/><node2/></root>');
if (is_array($nodes = $xml->xpath('node'))
    && sizeof($nodes) > 0
    && is_object($nodes[0])) {
    $nodes[0]['value'] = 'Hello';
    $nodes[0] = 'World';
$xml->node2 = 'World';
header('Content-Type: text/xml');
echo $xml->asXML();

Expected result:
<root><node value="Hello">World</node><node2>World</node2></root>

Actual result:
<root><node value="Hello"/><node2>World</node2></root>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-02 04:39 UTC]
I've come up with a workaround which imports the SimpleXML obkect into DOM and then converts back, but this, of course, defeats the purpose of SimpleXML. For reference I will post it here for others who need a solution to this problem:

$domnode = dom_import_simplexml($xml);
$dom = new DOMDocument();
$domnode = $dom->importNode($domnode, true);
$x = new DOMXPath($dom);
$n = $x->evaluate('/root/node');
while ($n->item(0)->firstChild) {
$xml = simplexml_import_dom($dom);
 [2006-06-20 14:25 UTC]
Marcus, could you pla take a look at it?
 [2006-12-30 13:42 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

You have to use DOM there. All you were doing was replacing the item at index 0 in the $nodes array with the string "World".
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 25 09:01:31 2024 UTC