我正在输出全部压缩在一起的 html,并希望将其转换为具有适当的缩进。我一直在尝试为此使用 xmllint,但没有任何乐趣。例如。当它在 file.html 中时:
<table><tr><td><b>Foo</b></td></tr></table>
<table><tr><td>Bar</td></tr></table>
我得到:
$ 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] >>>
但是当 file.html 单独包含其中任何一行时,它工作正常(删除第二行):
$ xmllint --format file.html
<?xml version="1.0"?>
<table>
<tr>
<td>
<b>Foo</b>
</td>
</tr>
</table>
当我包含 --html
选项时,它更有可能在没有错误的情况下运行,但它不会缩进。
有什么建议吗?我可以使用其他 (*nix) 工具吗?谢谢...
最佳答案
正如用户 4M01 所建议的:在命令行中,在管道中附加对 HTML tidy 的调用。
xmllint 的 HTML 输出将被修复; tidy 将围绕您的 html 片段包装一些合理的 ...。
xmllint --xpath "//tr[6]/td[7]" --html - | tidy -q
关于html - 如何使用 xmllint 缩进 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9197660/