Yet another solution:
use XML::Rules;
my $parser = XML::Rules->new(
rules => [
item => sub { '<li>' . $_[1]->{_content} . '</li>'},
list => sub {
for ($_[1]->{_content}) {
s/^\s+//;
s/\s+$//;
s{</li>\s+<li>}{</li><li>}g;
}
"\nTX <ol>" . $_[1]->{_content} . "</ol>\n"
},
para => sub { 'TX ' . $_[1]->{_content}},
head => sub { 'TX <h3>' . $_[1]->{_content} . "</h3>"},
'doc.body' => 'content',
]
);
use Data::Dumper;
my $result = $parser->parse($XML)->{'doc.body'};
for ($result) {
s/^\s+//;
s/\s+$//;
}
print $result;
__END__
C:\temp>c:\temp\reformatXML.pl
TX <h3>Introduction</h3>
TX para 1
TX para 2
TX <h3>Individual</h3>
TX para 1
TX para 2:
TX <ol><li>item 1</li><li>2</li></ol>
Getting rid of the excess whitespace complicates the code somewhat.