Just last week I've held a comprehensive tutorial on parsing
HTML using SGML, available online here:
http://sgmljs.net/docs/sgml-html-tutorial.html
This also includes a section on producing XHTML proper
from HTML.
SGML (unless using DSDL-9) will treat an XML namespace
binding attribute "xmlns:xyz" as literal attribute "xmlns:xyz"
and keep it intact, if that's what you're after. More generally,
SGML provides a highly customizable mechanism to integrate
HTML into XML processing pipelines based on an international
standard rather than on heuristics and best-effort approaches.
Cheers,
Marcus Reichardt
sgmljs.net