The code you posted has errors, and you didn't post any sample input, which makes it difficult to reproduce your problem.
I think part of the reason you're having trouble is hinted at in XML::Twig:
Pretty printing (at least using the 'indented' style) is hard to get right! Only elements that belong to the document will be properly indented. Printing elements that do not belong to the twig makes it impossible for XML::Twig to figure out their depth, and thus their indentation level.
Removing the pretty_print option seems to work for me; you could also try replacing it with keep_spaces => 1.
Otherwise, you might consider doing a second pass on the document to re-indent it after you've modified it; I'm not sure if XML::Twig will allow you to do it in a single pass, perhaps someone more versed in that module knows better.
In reply to Re: How to remove spaces after modifying an xml sheet.
by Anonymous Monk
in thread How to remove blank lines(new lines) after modifying an xml sheet.
by 92sai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |