If the HTML is well-formed (i.e. XHTML) then you can use XSLT to transform it. But of course it will need completely different XSLT code. And don't expect that every transformation is fully reversible - after all, you can compute 2+2=4, but you can't reverse that to convert 4 to 2+2.
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference