in reply to Hierarchical tag closing
First use a parser (e.g. HTML::TreeBuilder, XML::Twig) to create a data structure (I'd suggest a hash of hashes, aka HoH) to represent the data. Then write a function to traverse your HoH and create a new HoH structured the way you like it. Finally, traverse your new HoH and emit the text.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In Section
Seekers of Perl Wisdom