Question

I'm outputting html that's all crushed together, and would like to convert it to have proper indentation. I've been trying to use xmllint for this, but with no joy. E.g. when this is in file.html:

<table><tr><td><b>Foo</b></td></tr></table>
<table><tr><td>Bar</td></tr></table>

I get:

$ xmllint --format file.html
file.html:2: parser error : Extra content at the end of the document
<table><tr><td>Bar</td></tr></table>
^
<<< exit status [1] >>>

But when file.html contains either of those lines alone, it works fine (removing the second line):

$ xmllint --format file.html
<?xml version="1.0"?>
<table>
  <tr>
    <td>
      <b>Foo</b>
    </td>
  </tr>
</table>

When i inlcude the --html option, it's more likely to run without errors, but then it doesn't indent.

Any suggestions? Are there any other (*nix) tools I can use for this? Thanks ...

Was it helpful?

Solution

I think this is because the HTML you have supplied doesn't have a root tag, thus making it an invalid XML.

Try adding the body tag and run xmllint again on it.

<body><table><tr><td><b>Foo</b></td></tr></table>
<table><tr><td>Bar</td></tr></table></body>

OTHER TIPS

As user 4M01 suggested: On the command line, append the pipe with a call to HTML tidy.

HTML output from xmllint will be repaired; tidy will wrap some reasonable ... around your html fragment.

xmllint --xpath "//tr[6]/td[7]" --html - | tidy -q

tidy -i sets the indent: auto config value. If instead of auto I set it to yes, I consistently got better indentation style:

tidy --indent yes 

Have you tried HTML Tidy ? More Information about this is available at W3 & sourceforge.Even there GUI tool available which known as GuiTidy . This tools are great , they not only help in proper indentation but also validate html code.

Hope this help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top