XPath is able to find nodes without needing to know the position in the tree.
use strictures;
use XML::LibXML qw();
my $dom = XML::LibXML->load_xml(string => <<'XML');
ABC
XML
for my $id ($dom->findnodes('//id[string()="ABC"]')) {
$id->removeChildNodes;
$id->appendText('DEF');
}
print $dom->toString