我有这个dtd: http://fast-code.sourceforge.net/template.dtd 但是当我在xml中包含时,我得到警告: 没有为文档检测到语法约束(DTD或XML模式)。 xml是:

<?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>

编辑:我改变了xml,我现在得到这个错误:

元素类型“template”的内容必须匹配“(description,variation?,variation-field?,允许 multiple-variation ?,阶级结构? getter-setter ? allowed-file-extensions吗?阈值- 类?模板体内)”。


当前回答

答:

下面每个DTD部分的注释。更多信息请参考官方规范。

  <!
  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.
  >

简单的解释:

一个非常基本的DTD看起来就像这里的第二行:

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

详细解释:

dtd用于建立商定的数据格式并验证此类数据的接收。它们定义了XML文档的结构,包括:

法律要素清单 特殊字符 字符串 还有更多

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

其他回答

在记事本中复制您的整个代码。 临时以任何名称保存文件[在保存文件时使用"encoding" = UTF-8(或更高,但UTF)]。 关闭文件。 再打开一遍。 复制粘贴到代码中。

错误必须消失。

我在eclipse中使用web.xml文件时也遇到了同样的问题 它向我展示了这个“文档中没有引用语法约束”

但可以通过添加标签来解决 在XML标记之后,即<?xml version = "1.0" encoding = "UTF-8"?>

我在xsi:noNamespaceSchemaLocation中使用了相对路径来提供本地xsd文件(因为我不能在实例xml中使用名称空间)。

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

验证有效,警告被修复(没有被忽略)。

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

下面是这个问题的有效解决方案:


步骤1:右键单击项目并进入属性

步骤2:转到“libraries”,删除项目的“JRE系统库”

步骤3:点击“添加库”——>“JRE系统库”——>选择“工作区默认JRE”

步骤3:转到“订购和导出”,并标记新添加的“JRE系统库”

步骤4:刷新和清理项目

尤里卡!它正在工作:)

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