我有这个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 " ">
<!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 to   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
您是否尝试将模式添加到xml目录?
在eclipse中,为避免“没有为文档检测到语法约束(dtd或XML模式)”,我使用XSD模式文件添加到下的XML编目
“窗口\首选项\ xml \ xml目录\用户指定的条目”。
点击右侧的“添加”按钮。
例子:
<?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>
从这个xml中我生成并保存了一个xsd: /home/my_user/xsd/my_xsd.xsd
As位置:/home/my_user/xsd/my_xsd.xsd
As键类型:命名空间名称
作为键:http://mycompany.com/hr/schemas
关闭并重新打开xml文件并做一些更改来违反模式,您应该会收到通知