in reply to XML-Twig - AS5.6.1 vs AS 5.8.8

$element->delete() does not take an argument, it deletes the element itself, I think you intend for those deletes to be more like (this takes care of all in one go):
# Delete BAL_* nodes $balance->cut_children(qr/^BAL_\d+$/);
Note also that you are not deleting the BAL_30 and BAL_60 nodes, so you end up with two each of those (the originals and the ones you create with attributes). The cut_children() above assumes you didn't want the duplicates.