如何验证 HTML 是否符合 W3C 标准
我有一个使用 Velocity 模板和 Java 生成 HTML 页面的项目。但大多数页面不符合 W3C 标准。如何验证这些 HTML 页面并获取日志来告诉我哪些页面上有哪些错误/警告?
然后我可以手动修复错误。我尝试过 JTidyFilter,但这对我不起作用。
I have a project that generates HTML pages using a Velocity template and Java. But most of the pages do not comply with W3C standards. How can I validate those HTML pages and get a log telling me what errors/warnings on what pages?
Then I can fix the errors manually. I have tried JTidyFilter, but that doesn't work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以直接从 Java 使用 W3C 验证器,请参阅 w3c-jabi。
You can use the W3C validator directly from Java, see w3c-jabi.
W3C 还提供了一个实验性 API 来帮助自动化验证。他们恳请您限制请求,并提供有关在本地服务器上设置验证器的说明。这肯定需要更多工作,但如果您要生成大量 HTML 页面,那么自动化验证可能会更有意义。
http://validator.w3.org/docs/api.html
There is also an experimental API available from W3C to help automate validation. They kindly ask that you throttle requests, and also offer instructions on setting up a validator on a local server. It's definitely more work, but if you're generating a lot of HTML pages, it would probably make sense to also automate the validation.
http://validator.w3.org/docs/api.html
经过广泛的研究和一些代码修改,我成功地在我的项目中使用 JTidyFilter,并且它现在工作得很好。 JTidyFilter 位于 JTidyServlet 中,JTidyServlet 是大约五年前编写的 JTidy 的子项目。最近他们更新了代码以符合 Java 5 编译器。我下载了他们的代码,升级了一些依赖项,最重要的是,更改了 JTidyFilter 类中处理过滤器的一些行,最终让它在我的项目中正常工作。
重新格式化 HTML 时仍然存在一些问题,因为当我使用 Firefox HTML 验证插件时,我可以看到一两个错误,但大多数页面都通过了验证。
After extensive research and a little bit code hack, I've managed to use JTidyFilter in my project, and it is working beautifully now. JTidyFilter is in JTidyServlet which is a sub-project of JTidy written about five years ago. Recently they've updated the codes to comply with Java 5 compiler. I downloaded their codes, upgraded some dependencies and most importantly, changed some lines in the JTidyFilter class which handles the filter and finally got it work nicely in my project.
There are still some issues in reformatting the HTML, because I can see one or two errors when I use the Firefox HTML validation plugin, but otherwise most pages pass the validation.
官方 API
自 2007 年起允许通过标记验证器 Web 服务 API 调用本地或远程 W3C 检查器。
有使用 Jersey 和 moxy-Jaxb 读取 SOAP 响应的单个 Java 类解决方案。
这是使用它的 Maven 依赖项:
这是一个用于尝试它的 JUnit 测试:
展示了如何运行您的Ubuntu Linux 系统上的 W3C 验证器。
The official API at
allows to call a local or remote W3C checker via the Markup Validator Web Service API since 2007.
has a single Java class solution using Jersey and moxy-Jaxb to read in the SOAP response.
This is the Maven dependency to use it:
Here is a JUnit test for trying it:
shows how to run your on W3C validator on an Ubuntu Linux system.