Java web.xml 的虚假 Eclipse 警告:“未检测到文档的语法约束(DTD 或 XML 模式)。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/322155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:09:57  来源:igfitidea点击:

Bogus Eclipse warning for web.xml: "No grammar constraints (DTD or XML schema) detected for the document."

javaeclipsexsdwarnings

提问by Brian Deacon

The top of my web.xmlfile looks like this:

我的web.xml文件顶部如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
    version="2.5">

But I still get the warning from Eclipse (Ganymede) that no XML schema is detected, and schema violations are not being warned about. Other XML files in my project (Spring Frameworkconfiguration files for example) don't have the warning and do give correct warnings about schema violations.

但是我仍然收到来自 Eclipse (Ganymede) 的警告,即没有检测到 XML 模式,并且没有警告模式违规。我的项目中的其他 XML 文件(例如Spring Framework配置文件)没有警告,并且会给出有关架构违规的正确警告。

How do I get the schema checking working and hopefully the warning to go away? The server does run correctly. It just appears to be an IDE issue.

如何让模式检查工作并希望警告消失?服务器运行正常。它似乎只是一个IDE问题。

采纳答案by toolkit

Perhaps try:

也许尝试:

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

Instead of:

代替:

http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd


Also, the <!DOCTYPE ...>is missing:

此外,<!DOCTYPE ...>缺少:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app
  xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">
  <!-- ... -->
</web-app>

回答by PHP-Rocks

Add this <!DOCTYPE ...>to your xml file. Please put it under <?xml ...>:

将此添加<!DOCTYPE ...>到您的 xml 文件中。请放在下面<?xml ...>

<!DOCTYPE ??? PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

???= Your root element, now if your sub-element name is using a html reserved word you might be catching some errors, all you have to do is change them to a non-reserved word.

???= 您的根元素,现在如果您的子元素名称使用 html 保留字,您可能会发现一些错误,您所要做的就是将它们更改为非保留字。

For example:
If your current sub-element is <img>, change it to <pic>...

例如:
如果您当前的子元素是<img>,请将其更改为<pic>...

回答by monzonj

I hate that warning too. Specially because it appears in XML files that you haven't written but appear in your project for whatever reason (if you use MAVEN it's hell).

我也讨厌那个警告。特别是因为它出现在您尚未编写的 XML 文件中,但出于任何原因出现在您的项目中(如果您使用 MAVEN,那就太糟糕了)。

With Eclipse 3.5+ you can easily remove this validation rule. Go to Preferences-->XML-->XML FILES --> Validation and Select "ignore".

使用 Eclipse 3.5+,您可以轻松删除此验证规则。转到首选项--> XML--> XML 文件--> 验证并选择“忽略”。

You may also have to do a Project -> Clean for the validation warnings to go away.

您可能还需要执行 Project -> Clean 以使验证警告消失。

alt text

替代文字

回答by Tiris

Clear the cache for stored validation files.

清除存储验证文件的缓存。

Window > Preferences > General > Network Connections > Cache then remove all. Now go validate the file and see if that clears things up.

Window > Preferences > General > Network Connections > Cache 然后全部删除。现在去验证文件,看看是否可以解决问题。

This happed to me and clearing the cache for the validation was the only way to get it functioning properly again. The advice for clearing dirty cache was found here.

这发生在我身上,清除缓存以进行验证是使其再次正常运行的唯一方法。在这里找到了清除脏缓存的建议。

回答by eckes

If you have the same (missleading) error message because your XML Editor was not finding the XSD file, you can add a catalog entry.

如果由于 XML 编辑器未找到 XSD 文件而出现相同(误导性)错误消息,则可以添加目录条目。

You pick the URL specified for the schema, for a declaration like

您选择为架构指定的 URL,用于声明如下

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                      http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"

The URL of the schema file (http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd) is specified for the namespace http://java.sun.com/xml/ns/j2ee. You can now redirect the location of the file with the workspace catalog in Eclipse:

架构文件的 URL (http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd) 是为命名空间http://java.sun.com/xml/ns/j2ee指定的。您现在可以使用 Eclipse 中的工作区目录重定向文件的位置:

Preferences -> XML -> XML Catalog -> Add..

Use

Key Type = Schema Location
Key = http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd

And then you can use the file chooser to pick actually a XSD file on the filesystem or the workspace.

然后您可以使用文件选择器实际选择文件系统或工作区上的 XSD 文件。

回答by Thorbj?rn Ravn Andersen

The problem is two-fold:

问题有两个:

  1. Eclipse ships with a cache of a lot of well-known XSD's. The name space and/or location you provide does not match any of these.
  2. So Eclipse tries to go looking for the XSD on the internet on the URI provided (may just work). Unfortunately it appears that after Oracle revised java.sun.com this mechanism just times out in Eclipse (apparently the server redirects to the homepage instead of simply saying "does not exist, sorry").
  1. Eclipse 附带了许多众所周知的 XSD 的缓存。您提供的名称空间和/或位置与其中任何一个都不匹配。
  2. 因此,Eclipse 尝试在提供的 URI 上在 Internet 上查找 XSD(可能会起作用)。不幸的是,在 Oracle 修改 java.sun.com 之后,这种机制似乎在 Eclipse 中超时(显然服务器重定向到主页而不是简单地说“不存在,抱歉”)。

When you revise to the correct values for Java EE 5, the entry in the cache will be found and Eclipse will be happy.

当您修改 Java EE 5 的正确值时,将在缓存中找到该条目并且 Eclipse 会很高兴。