Download and install Tidy.
For help, do:
tidy -help
To clean up HTML by creating indented code, wrapping text at column #500, converting HTML to XHTML, and modify the existing file in place, do the following:
tidy -i -w 500 -asxml -m <filename>
Make sure to make a backup copy of the file first.
Now remove blank lines from the resulting cleaned-up code using perl:
perl -pi -e "s/^\n//" <filename>