Mori
Mori

Reputation: 27789

How to indent html with xmllint?

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 ...

Upvotes: 4

Views: 9469

Answers (4)

Edward Anderson
Edward Anderson

Reputation: 13916

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 

Upvotes: 4

knb
knb

Reputation: 9295

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

Upvotes: 4

Amol Chavan
Amol Chavan

Reputation: 3980

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

Upvotes: 1

adarshr
adarshr

Reputation: 62593

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>

Upvotes: 2

Related Questions