note 56120 modified in function.simplexml-element-xpath by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Sep 2009 15:54:29 +0000 |
| Subject: | note 56120 modified in function.simplexml-element-xpath by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160695@lists.php.net to get a copy of this message | ||
Xpath actually returns an array full of references to elements in the parent SimpleXML object.
Therefore, in addition to preserving attributes, you can actually modify them and the result will be
reflected in the parent.
Example:
<?php
$xml_str = <<<END
<a>
<b attr="foo"/>
</a>
END;
$xml = simplexml_load_string ($xml_str);
$result = $xml->xpath ('/a/b'); // returns an array with one element
$result[0]['attr'] = 'bar';
print $xml->asXML ();
?>
Outputs:
<a>
<b attr="bar"/>
</a>
--was--
Xpath actually returns an array full of references to elements in the parent SimpleXML object.
Therefore, in addition to preserving attributes, you can actually modify them and the result will be
reflected in the parent.
Example:
<?php
$xml_str = <<<END
<a>
<b attr="foo"/>
</a>
END;
$xml = simplexml_load_string ($xml_str);
$result = $xml->xpath ('/a/b'); // returns an array with one element
$result[0]['attr'] = 'bar';
print $xml->asXML ();
?>
Outputs:
<a>
<b attr="bar"/>
</a>
http://php.net/manual/en/function.simplexml-element-xpath.php