Most people use the DOMElement::setAttribute() or DOMElement::setAttributeNS() method for this.
$xml = <<<XML
<elements>
<element name="agents" path="admin/agents" scope="system">
...
</element>
</elements>
XML;
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
foreach ($xpath->evaluate('//element[1]') as $element) {
$element->setAttribute('visibility', 'hidden');
}
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<elements>
<element name="agents" path="admin/agents" scope="system" visibility="hidden">
...
</element>
</elements>
However attributes are nodes, too. So you can create them using DOMDocument::createAttribute() and set using DOMElement::setAttributeNode(). This allows to separate the creation from the assignment.
foreach ($xpath->evaluate('//element[1]') as $element) {
$attribute = $document->createAttribute('visibility');
$attribute->value = 'hidden';
$element->setAttributeNode($attribute);
}