I remember correctly, XML::Twig has options that control the processing of comments. 'keep' writes them in the output, and 'process' adds them to the tree as special elements.
Any ideas on why it is reformatting the prolog: it added standalone="no" to the xml declaration, and in the DOCTYPE changes PUBLIC to SYSTEM even if I disable DTD loading with