Not sure what you want to do exactly, but the general idea is :
Here, for example, you could use some thing like this :
$str = <<<XML
<collections id="My Collections">
<category id="my category">
<record id="my record">
<title>Some Info</title>
</record>
</category>
</collections>
XML;
$dom = new DOMDocument();
$dom->loadXML($str);
$xpath = new DOMXPath($dom);
$elements = $xpath->query('//record[@id="my record"]');
if ($elements->length >= 1) {
$element = $elements->item(0);
$element->setAttribute('id', "glop !");
}
echo '<pre>' . htmlspecialchars($dom->saveXML()) . '</pre>';
This will replace the id
attribute my record
, on the node that's identified by it, by "glop !
", and you'd get the following XML as output :
<?xml version="1.0"?>
<collections id="My Collections">
<category id="my category">
<record id="glop !">
<title>Some Info</title>
</record>
</category>
</collections>