You can create an array of all nodes in the document using the SimpleXMLElement::xpath() method.
Then you can use array_walk in this array. However, you do not want to change the row of each node only for those elements that do not have children.
$source = ' <api> <stuff>ABC</stuff> <things> <thing>DEF</thing> <thing>GHI</thing> <thing>JKL</thing> </things> </api> '; $xml = new SimpleXMLElement($source); array_walk($xml->xpath('//*'), function(&$node) { if (count($node)) return; $node[0] = strrev($node); }); echo $xml->asXML();
The above example displays the following:
<?xml version="1.0"?> <api> <stuff>CBA</stuff> <things> <thing>FED</thing> <thing>IHG</thing> <thing>LKJ</thing> </things> </api>
The xpath query allows more control, for example, with namespaces.
hakre source share