如何验证HTML是否符合W3C标准

5
我有一个项目,使用Velocity模板和Java生成HTML页面。但是大多数页面不符合W3C标准。我该如何验证这些HTML页面,并获取告诉我哪些页面上有什么错误/警告的日志?
然后我可以手动修复错误。我尝试过JTidyFilter,但对我没有用。
4个回答

5
你可以直接从Java使用W3C验证器,参见w3c-jabi

2

W3C还提供了一个实验性API来帮助自动验证。他们友好地要求您限制请求,并提供有关在本地服务器上设置验证器的说明。这肯定需要更多的工作,但如果您正在生成大量HTML页面,自动验证也是非常有意义的。

http://validator.w3.org/docs/api.html


1
我很惊讶Java没有API。除此之外,我真的不想修改源代码来添加API。我只想在我的J2EE项目中更改配置文件,以便在开发时打开它,在不需要时关闭它。 - newguy
请在其他答案中查看Java解决方案。 - Wolfgang Fahl

1
经过广泛的研究和一些代码修改,我成功地在我的项目中使用了JTidyFilter,并且现在它运行得非常好。JTidyFilter是JTidyServlet的一个子项目,大约五年前编写。最近,他们更新了代码以符合Java 5编译器的要求。我下载了他们的代码,升级了一些依赖项,最重要的是,在处理过滤器的JTidyFilter类中更改了一些行,最终使其在我的项目中完美地工作。
在重新格式化HTML方面仍然存在一些问题,因为当我使用Firefox HTML验证插件时,我可以看到一两个错误,但除此之外,大多数页面都通过了验证。

0

官方API位于

自2007年以来,允许通过Markup Validator Web Service API调用本地或远程W3C检查器。

有一个使用Jersey和moxy-Jaxb的单个Java类解决方案来读取SOAP响应。

这是使用它的Maven依赖项:

<dependency>
  <groupId>com.bitplan</groupId>
  <artifactId>w3cValidator</artifactId>
  <version>0.0.2</version>
 </dependency>

这是一个用于尝试的JUnit测试:

/**
 * The URL of the official W3C markup validation service.
 * If you'd like to run the tests against your own installation you might want to modify this.
 */
public static final String url = "http://validator.w3.org/check";

/**
 * Test the w3cValidator interface with some HTML code
 * @throws Exception
 */
@Test
public void testW3CValidator() throws Exception {

    String preamble =
            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" +
            "   \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
            "<html>\n" +
            "  <head>\n" +
            "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" +
            "    <title>test</title>\n" +
            "  </head>\n" +
            "  <body>\n";

    String footer = "  </body>\n" +
            "</html>\n";

    String[] htmls = {
            preamble +
            "    <div>\n" +
            footer,
            "<!DOCTYPE html><html><head><title>test W3CChecker</title></head><body><div></body></html>"
    };
    int[] expectedErrs = {1, 2};
    int[] expectedWarnings = {1, 2};
    int index = 0;
    System.out.println("Testing " + htmls.length + " html messages via " + url);
    for (String html : htmls) {
        W3CValidator checkResult = W3CValidator.check(url, html);
        List<ValidationError> errlist = checkResult.body.response.errors.errorlist;
        List<ValidationWarning> warnlist = checkResult.body.response.warnings.warninglist;
        Object first = errlist.get(0);
        assertTrue("if first is a string, than moxy is not activated",
                   first instanceof ValidationError);
        //System.out.println(first.getClass().getName());
        //System.out.println(first);
        System.out.println("Validation result for test " + (index+1) + ":");
        for (ValidationError err:errlist) {
            System.out.println("\t" + err.toString());
        }
        for (ValidationWarning warn:warnlist) {
            System.out.println("\t" + warn.toString());
        }
        System.out.println();
        assertTrue(errlist.size() >= expectedErrs[index]);
        assertTrue(warnlist.size() >= expectedWarnings[index]);
        index++;
    }
} // testW3CValidator

展示如何在Ubuntu Linux系统上运行您自己的W3C验证器。


它已经过时了 - 它使用的是SOAP API,该API已被W3C弃用。 - Michal_Szulc
@Michal_Szulc 谢谢你的提醒。我自2018年以来就没有更新http://wiki.bitplan.com/index.php/W3cValidator了。它为我们服务了几年。你知道有什么替代品吗? - Wolfgang Fahl
我找到了这篇文章:https://vzurczak.wordpress.com/2015/03/16/validating-a-html-page-with-java/。它推荐使用https://validator.github.io/validator/,但是`com.jcabi:jcabi-w3c`似乎更简单易用且有效。 - Michal_Szulc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接