Cleaning up HTML.

exercise No. 18

Create comment in forum


Consider the following HTML legacy document:

<html xmlns=''>
    <title>A simple image example</title>
    <img src='a.gif' align='right'/> <!-- Error:  pre- HTML5 style -->

    <p>Some inline image without alignment <img src="b.gif"/></p>
    <p>Some inline image with alignment <img src="c.gif" align="bottom"/><!-- Error:  pre- HTML5 style --></p>

The pre-HTML5 align='...'attribute is deprecated and has been replaced by CSS style="vertical-align: ...;" or style="float: ...;" respectively:

<html xmlns="">
    <title>A simple image example</title>
    <img src="a.gif" style="float: right" />

    <p>Some inline image without alignment <img src="b.gif" /></p>
    <p>Some inline image with alignment <img src="c.gif" style="vertical-align: bottom;" /></p>

Write a JDom based filter application which transforms these legacy declarations to CSS accordingly.


A possible road map:

  1. Start by an identity transformation: Parse your HTML document to a DOM tree and simply serialize this tree to standard output.

  2. Modify the intermediate DOM tree. The recursive descent method from the section called “Visualizing XML document elements” allows for retrieving all <img .../> elements.

  3. The getAttribute(...) method allows for identifying relevant <img align='...'/> elements.

  4. You may then modify <img align='...'/> elements using removeAttribute(...) and setAttribute(...).

Optional: Supply an XSLT doing the same job as your Java application and compare both solution variants. You may want to read The Identity Transform. This enables you to:

  1. Copy most HTML from input to output like in your Java solution.

  2. Handle relevant <img align='...'/> elements separately by defining an extra template.


The above solution contains a remarkably small html2html.xsl style sheet.


Both solution variants do not account for elements <img ... align='...' style='...'/> already defining a style attribute. Any such existing value will be overridden. It is however straightforward extending the current solution to append to the style attributes value rather then overriding it.