Note that changing attributes from within a foreach loop, especially namespaced attributes, can be very tricky.
For example, when trying to change the value of an existing xlink:href attribute:
<?php
foreach($xmlelement -> attributes('xlink', true) as $attribute => $attribvalue){
$attribvalue[0] = 'value'; // Throws an error
$attribvalue = 'value'; // Does not change your XML
$xmlelement -> addAttribute($attribute, 'value', 'http://www.w3.org/1999/xlink'); // Adds an attribute, does not change existing one.
$xmlelement[$attribute] = 'value'; // Adds an attribute, does not change existing one.
}
?>
Instead, you should access the array returned by the attributes() function directly, like this:
<?php
$xmlelement -> attributes('xlink', true) -> href = 'value'; // Works!
?>