如果DTD是包含在文档中,则需要定义,格式:
<!DOCTYPE root-element [element-declarations]>
eg:
<?xml version="1.0"?>
<!DOCTYPE note [---------------------------------------------------------------//说明这个文档是说明类型node的
<!ELEMENT note (to,from,heading,body)>----------------------------//说明node节点拥有4个子节点
<!ELEMENT to (#PCDATA)>------------------------------------------//说明节点to的类型
<!ELEMENT from (#PCDATA)>----------------------------------------//同上
<!ELEMENT heading (#PCDATA)>--------------------------------------//同上
<!ELEMENT body (#PCDATA)>----------------------------------------//同上
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
如果DTD是在文件外部定义的:
在xml文件中声明:<!DOCTYPE note SYSTEM "note.dtd">指定文件note.dtd,文件内容为
<!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
从dtd的观点看整个xml文件的结构,分为下面几个块
- Elements ------------------//节点
- Attributes ----------------//属性
- Entities ------------------//实体,如<(<),>(>),&(&),"(“),&apos(')
- PCDATA --------------------//字符,将得到XML分析器的分析,同DHTML中的innerHTML
- CDATA ---------------------//字符,不能得到XML分析器的分析,同DHTML中的innerText
Elements:
声明一个节点:
<!ELEMENT element-name category> or <!ELEMENT element-name (element-content)>
空节点:<!ELEMENT br EMPTY>
只含有字符数据的节点:<!ELEMENT from (#PCDATA)>
拥有随意内容的节点:<!ELEMENT note ANY>
拥有子节点的节点:<!ELEMENT note (to,from,heading,body)>
定义只出现一次的子节点:<!ELEMENT note (message)>message 在note中只允许出现一次
定义子节点至少出现一次:<!ELEMENT note (message+)>message 在note中至少出现一次
定义子节点出现零次或多次:<!ELEMENT note (message*)>
定义子节点出现0次或1次:<!ELEMENT note (message?)>
定义出现的[或]关系:<!ELEMENT note (to,from,header,(message|body))>
定义:<!ELEMENT note (#PCDATA|to|from|header|message)*>
Attributes:
定义属性:<!ATTLIST element-name attribute-name attribute-type default-value>
<!ATTLIST payment type CDATA "check">-------<payment type="check" />
attribute-type列表
Value | Explanation |
---|---|
CDATA |
The value is character data字符 |
(en1|en2|..) |
The value must be one from an enumerated list枚举 |
ID |
The value is a unique id 唯一,类似于主键 |
IDREF |
The value is the id of another element类似于外键 |
IDREFS |
The value is a list of other ids |
NMTOKEN |
The value is a valid XML name 一个合法的xml名字 |
NMTOKENS |
The value is a list of valid XML names |
ENTITY |
The value is an entity 一个实体 |
ENTITIES |
The value is a list of entities |
NOTATION |
The value is a name of a notation |
xml: |
The value is a predefined xml value |
Value | Explanation |
---|---|
value |
The default value of the attribute |
#REQUIRED |
The attribute value must be included in the element 必须有此属性 |
#IMPLIED |
The attribute does not have to be included 可有可无的属性,没有default value选项 |
#FIXED value |
The attribute value is fixed 属性的固定值 |
eg: dtd: <!ATTLIST payment type (check|cash) "cash">
xml: <payment type="check" />
or
<payment type="cash" />
Entity:
内部实体声明:
语法:
<!ENTITY entity-name "entity-value"> DTD 例子:
<!ENTITY writer "Donald Duck."> <!ENTITY copyright "Copyright W3Schools.">
XML 例子:
<author>&writer;©right;</author>
外部实体声明:
语法:
<!ENTITY entity-name SYSTEM "URI/URL"> DTD 例子:
<!ENTITY writer SYSTEM "http://www.w3schools.com/dtd/entities.dtd"> <!ENTITY copyright SYSTEM "http://www.w3schools.com/dtd/entities.dtd">
XML 例子:
<author>&writer;©right;</author>
javascript验证xml文件是否合法,并输出错误信息:
<html>
<body>
<h3>
This demonstrates a parser error:
</h3>
<script type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="true"
xmlDoc.load("note_dtd_error.xml")
document.write("<br />Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br />Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br />Error Line: ")
document.write(xmlDoc.parseError.line)
</script>
</body>
</html>
样例提供:(w3提供)
http://www.w3schools.com/dtd/dtd_examples.asp