227

I have this dtd : http://fast-code.sourceforge.net/template.dtd But when I include in an xml I get the warning : No grammar constraints (DTD or XML schema) detected for the document. The xml is :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE templates PUBLIC "//UNKNOWN/" "http://fast-code.sourceforge.net/template.dtd">

<templates>
<template type="INSTANCE_OF_CLASS">
    <description>Used to Create instance of class</description>
    <variation>asasa</variation>
    <variation-field>asasa</variation-field>
    <class-pattern>asasa</class-pattern>
    <getter-setter>setter</getter-setter>
    <allowed-file-extensions>java</allowed-file-extensions>
    <number-required-classes>1</number-required-classes>
    <allow-multiple-variation>false</allow-multiple-variation>
    <template-body>
        <![CDATA[
            // Creating new instance of ${class_name}
            final ${class_name} ${instance} = new ${class_name}();
            #foreach ($field in ${fields})
                ${instance}.${field.setter}(${field.value});
            #end
        ]]>
    </template-body>
</template>
</templates>

EDIT : I changed the xml, I am getting this error now:

The content of element type "template" must match "(description,variation?,variation-field?,allow- multiple-variation?,class-pattern?,getter-setter?,allowed-file-extensions?,number-required- classes?,template-body)".

bluish
  • 26,356
  • 27
  • 122
  • 180
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • 4
    Check these links: [No grammar constraints (DTD or XML schema) detected for the document](http://argillander.wordpress.com/2011/06/14/no-grammar-constraints-dtd-or-xml-schema-detected-for-the-document/) and [Bogus Eclipse warning for web.xml](http://stackoverflow.com/questions/322155/bogus-eclipse-warning-for-web-xml-no-grammar-constraints-dtd-or-xml-schema-d). – nav Nov 29 '11 at 05:27
  • 8
    I would NOT do this **Preferences | XML | XML Files | Validation** in Eclipse as this only mask/hide the error, it does not _solve_ the error. If you only use Eclipse to build Android applications, then this "solution" is acceptable. But if you use Eclipse to build other Java projects - JSF, etc - it will break these projects if XML validation is "turned off". So be careful. The real solution is for Oracle, Google and IBM to update their softwares with updated DTDs and schemas. – ChuongPham Jan 18 '14 at 18:17
  • I find eclipse far faster and less crash-prone if I keep one install for working on android apps and another one for doing java web development work. So I'd be happy to use the solution above just for my android ADT install of eclipse. – Damon Smith Jun 27 '14 at 01:18

18 Answers18

452

I got rid of this annoying warning by specifying <!DOCTYPE xml> after the <?xml ... > tag instead of specifying something else (like templates in your case).

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • 32
    In the second line, " ", "xml" should match the name of the document's *root element*. – Pup Jan 02 '15 at 23:15
  • 2
    Downvote: the question was how to get eclipse to perform validation on the dtd that the asker had, not how to eliminate the warning. – Periata Breatta Oct 05 '16 at 11:00
  • 1
    Doesn't work. Eclipse 4.10, date 2019-05-31. Sometimes the warning goes away - but it comes back again when you relaunch Eclipse. – mike rodent May 31 '19 at 17:15
  • 1
    Two things: 1. your doctype declaration must match the root node name of the xml document. 2. The warning may persist until you perform explicit validation via right-click / Validate in the project explorer menu. – mvreijn Jun 19 '20 at 09:44
167

This worked for me in Eclipse 3.7.1:

Go to the Preferences window, then XML -> XML Files -> Validation.

Then in the Validating files section of the preferences panel on the right, choose Ignore in the drop down box for the "No grammar specified" preference. You may need to close the file and then reopen it to make the warning go away.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Marc Bernstein
  • 11,423
  • 5
  • 34
  • 32
  • 1
    I had to make sure that the "manual" box as well as the "build" box was checked and then do a manual validate by right-clicking on the project and selecting "Validate" for the warnings to go away. It seems that there is a bug in eclipse in which the validator gets messed up. You *should* be able to at least have the manual validation work even if you don't want it to happen at build time. And then you should be able to easily clear the warnings after doing the manual validation. Until then this is the solution. – crowmagnumb May 25 '12 at 19:22
  • 1
    The warning breaks intellisense and I spent quite a lot of time figuring what reason was. This resolved it :) – Shashank Shekhar May 19 '13 at 23:26
  • You can also do this on a project-by-project basis which makes the change in your .project file which you can check in source control and help your colleagues out! Instead of going to the Preferences window, right click on your project and go to Properties and follow the instructions above from there. – Stuart Leyland-Cole Dec 05 '13 at 11:37
  • 4
    Downvote. The question asked is not how to hide the warning, but how to resolve it (i.e. make eclipse correctly validate the document).. – Periata Breatta Oct 05 '16 at 11:01
37

Answer:

Comments on each piece of your DTD below. Refer to official spec for more info.

  <!
  DOCTYPE ----------------------------------------- correct
  templates --------------------------------------- correct  Name matches root element.
  PUBLIC ------------------------------------------ correct  Accessing external subset via URL.
  "//UNKNOWN/" ------------------------------------ invalid? Seems useless, wrong, out-of-place.
                                                             Safely replaceable by DTD URL in next line.
  "http://fast-code.sourceforge.net/template.dtd" - invalid  URL is currently broken.
  >

Simple Explanation:

An extremely basic DTD will look like the second line here:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE nameOfYourRootElement>
<nameOfYourRootElement>
</nameOfYourRootElement>

Detailed Explanation:

DTDs serve to establish agreed upon data formats and validate the receipt of such data. They define the structure of an XML document, including:

  • a list of legal elements
  • special characters
  • character strings
  • and a lot more

E.g.

<!DOCTYPE nameOfYourRootElement
[
<!ELEMENT nameOfYourRootElement (nameOfChildElement1,nameOfChildElement2)>
<!ELEMENT nameOfChildElement1 (#PCDATA)>
<!ELEMENT nameOfChildElement2 (#PCDATA)>
<!ENTITY nbsp "&#xA0;"> 
<!ENTITY author "Your Author Name">
]>

Meaning of above lines...
Line 1) Root element defined as "nameOfYourRootElement"
Line 2) Start of element definitions
Line 3) Root element children defined as "nameOfYourRootElement1" and "nameOfYourRootElement2"
Line 4) Child element, which is defined as data type #PCDATA
Line 5) Child element, which is defined as data type #PCDATA
Line 6) Expand instances of &nbsp; to &#xA0; when document is parsed by XML parser
Line 7) Expand instances of &author; to Your Author Name when document is parsed by XML parser
Line 8) End of definitions

Pup
  • 10,236
  • 6
  • 45
  • 66
21

The Real Solution:

add <!DOCTYPE something> to the begining of each problematic XML,

after the xml tag <?xml version="1.0" encoding="utf-8"?>

you can write anything for doctype, but basically it's supposed to be manifest, activity, etc. from what I understand

Amir.F
  • 1,911
  • 7
  • 29
  • 52
  • Doesn't work. Eclipse 4.10, date 2019-05-31. Sometimes the warning goes away - but it comes back again when you relaunch Eclipse. – mike rodent May 31 '19 at 17:08
  • @mikerodent The warning may persist until you perform explicit validation via right-click / Validate in the project explorer menu – mvreijn Jun 19 '20 at 09:46
12

Have you tried to add a schema to xml catalog?

in eclipse to avoid the "no grammar constraints (dtd or xml schema) detected for the document." i use to add an xsd schema file to the xml catalog under

"Window \ preferences \ xml \ xml catalog \ User specified entries".

Click "Add" button on the right.


Example:

<?xml version="1.0" encoding="UTF-8"?>
<HolidayRequest xmlns="http://mycompany.com/hr/schemas">
    <Holiday>
        <StartDate>2006-07-03</StartDate>
        <EndDate>2006-07-07</EndDate>
    </Holiday>
    <Employee>
        <Number>42</Number>
        <FirstName>Arjen</FirstName>
        <LastName>Poutsma</LastName>
    </Employee>
</HolidayRequest>

From this xml i have generated and saved an xsd under: /home/my_user/xsd/my_xsd.xsd

As Location: /home/my_user/xsd/my_xsd.xsd

As key type: Namespace name

As key: http://mycompany.com/hr/schemas


Close and reopen the xml file and do some changes to violate the schema, you should be notified

zxmax
  • 151
  • 1
  • 3
  • This worked for me, but with this change. I was editing a .wadl file (see http://www.w3.org/Submission/wadl/ ) and the examples there use . I was only able to get this to work by using just xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02" and not xsi:schemaLocation="http://wadl.dev.java.net/2009/02 wadl.xsd" as the examples on that Submission show. I added a catalog entry for the namespace name with http://wadl.dev.java.net/2009/02 – djb Jul 27 '12 at 21:31
11

Add DOCTYPE tag ...

In this case:

<!DOCTYPE xml>

Add after:

<?xml version="1.0" encoding="UTF-8"?>

So:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
8

Try understanding the suggestion given in warning messages, it's helps sometime.

I also had the same issues, after doing some research i found the solution and fixed by adding the below line at the beginning

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>

which lead to some other issue in my case. Which i solved by reading the suggestions, which again turned into the another problem, which has been solved understanding the original cause, that the first link was broken (not found). Below are the step by step images which explains what has been done to resolve the issue completely.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Bhupendra Kumar
  • 179
  • 3
  • 7
7

A new clean way might be to write your xml like so:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE rootElement>

<rootElement>
 ....
</rootElement>

The above works in Eclipse Juno+

Damilola
  • 1,014
  • 2
  • 20
  • 22
  • The warning may persist until you perform explicit validation via right-click / Validate in the project explorer menu. – mvreijn Jun 19 '20 at 09:46
5

For me it was a Problem with character encoding and unix filemode running eclipse on Windows:

Just marked the complete code, cutted and pasted it back (in short: CtrlA-CtrlX-CtrlV) and everything was fine - no more "No grammar constraints..." warnings

user3022856
  • 259
  • 3
  • 2
4

I can't really say why you get the "No grammar constraints..." warning, but I can provoke it in Eclipse by completely removing the DOCTYPE declaration. When I put the declaration back and validate again, I get this error message:

The content of element type "template" must match "(description+,variation?,variation-field?,allow-multiple-variation?,class-pattern?,getter-setter?,allowed-file-extensions?,template-body+).

And that is correct, I believe (the "number-required-classes" element is not allowed).

mzjn
  • 48,958
  • 13
  • 128
  • 248
  • The parenthesis indicate that order is important. Your document has a allow-multiple-variation element after a class-pattern element, which is invalid.You really should change the question or accept an existing answer before asking a different one. – nitind Oct 14 '11 at 07:01
3

i know this is old but I'm passing trought the same problem and found the solution in the spring documentation, the following xml configuration has been solved the problem for me.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx 

<!-- THIS IS THE LINE THAT SOLVE MY PROBLEM -->
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

before I put the line above as sugested in this forum topic , I have the same warning message, and placing this...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>

and it give me the following warning message...

The content of element type "template" must match "
(description,variation?,variation-field?,allow- multiple-variation?,class-
pattern?,getter-setter?,allowed-file-extensions?,number-required- 
classes?,template-body)".

so just try to use the sugested lines of my xml configuration.

José Oliveira
  • 121
  • 1
  • 5
2

This may be due to turning off validation in eclipse.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
Chotka
  • 1,278
  • 2
  • 8
  • 8
1
  1. copy your entire code in notepad.
  2. temporarily save the file with any name [while saving the file use "encoding" = UTF-8 (or higher but UTF)].
  3. close the file.
  4. open it again.
  5. copy paste it back on your code.

error must be gone.

sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
1

I used a relative path in the xsi:noNamespaceSchemaLocation to provide the local xsd file (because I could not use a namespace in the instance xml).

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../project/schema.xsd">
</root>

Validation works and the warning is fixed (not ignored).

https://www.w3schools.com/xml/schema_example.asp

Max Hohenegger
  • 1,609
  • 15
  • 19
1

I too had the same problem in eclipse using web.xml file
it showed me this " no grammar constraints referenced in the document "

but it can be resolved by adding tag
after the xml tag i.e. <?xml version = "1.0" encoding = "UTF-8"?>

Stephan
  • 41,764
  • 65
  • 238
  • 329
1

Solved this issue in Eclipse 3.5.2. Two completely identical layouts of which one had the warning. Closed down all tabs and when reopening the warning had disappeared.

delformo
  • 1,001
  • 10
  • 7
0

Here's the working solution for this problem:


Step 1: Right click on project and go to properties

Step 2: Go to 'libraries' and remove the project's ' JRE system library'

Step 3: Click on 'Add library'-->'JRE System Library' -->select 'Workspace default JRE'

Step 3: Go to 'Order and Export' and mark the newly added ' JRE system library'

Step 4: Refresh and Clean the project

Eureka! It's working :)

  • I tried using your solution. But unfortunately, when I refresh and clean my project the newly added 'JRE System Library' disappears from my 'Libraries' section and also from my 'Order and Export' section. Please help. – VikramV Feb 18 '14 at 13:07
  • Most of this answer is unrelated to the question except for Step 4 "clean the project" which might (or might not) help. – Aaron Digulla Aug 29 '16 at 09:34
0

What I found to be the solution was something very very simple that I think you should try before tinkering with the preferences. In my case I had this problem in a strings file that had as a base tag "resources" ...all I did was delete the tag from the top and the bottom, clean the project, save the file and reinsert the tag. The problem has since disappeared and never gave me any warnings. It may sound to simple to be true but hey, sometimes it's the simplest things that solve the problems. Cheers

Francesco D.M.
  • 2,129
  • 20
  • 27