1.What is a DTD?
DTD : Document Type Definition. is to define the legal building blocks of an XML document.
2.Why Use a sDTD?
With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.
Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
(Metadata:data about data.)
<! ELEMENT element-name category> or <!ELEMENT element-name (element-content)>
Main building blocks.as HTML elements.
Eg : <message>some text</message>
A special elements is empty element.
Example in HTML like <hr />
Empty elements are declared with the category keyword “EMPTY”.
<! ELEMENT br EMPTY> ,template <!ELEMENT element-name EMPTY>
<!ATTLIST element-name attribute-name attribute-type attribute-value>
Eg: <payment type=”check” />
DTD example ---> <!ATTLIST payment type CDATA “check”>
Some characters have a special meaning in XML,like the less than sign(<) that defines the start of an XML tag. Most of you know the HTML entity:”&ngsp”.
This “no-breaking-space” entity is used in HTML to insert an extra space in a document.
Entities are expanded when a document is parsed by an XML parser.
There are entities are predefined in XML:
(< <),(&qt; >),(" “),(' ‘),(& &).
PCDATA is text that will be parsed by a parser.The text will be examined by the parser for entities and markup.
可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本。
PCDATA 是会被解析器解析的文本。这些文本将被解析器检查实体以及标记。
CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.
CDATA 是不会被解析器解析的文本。在这些文本中的标签不会被当作标记来对待,其中的实体也不会被展开。
If your XML documents has errors, and the XML Paser might generate an Errors,By accessing the parseError object,you can retrieve the error code, the error text, or even the line that caused the error.
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);
The best way is to avoid using attributes.Some of the problems with attributes are:
1.Attributes cannot contain multiple values(child element can);
2.Attributes are not easily espandable (for future changes);
3.Attributes cannot describe structures(child element can);
4. Attributes are not difficult to manipulate by program code;
5.Attributes values are not easy to test against a DTD.
A date attribute is used in the first example:
<note date="12/11/2002"> ...... </note> A date element is used in the second example: <note> <date>12/11/2002</date> .............. </note> An expanded date element is used in the third: (THIS IS MY FAVORITE): <note> <date> <day>12</day> <month>11</month> <year>2002</year> </date> ....... </note>
In a word:metadata should be stored as attributes,and that data itself should be stored as elements.
Newspaper Article DTD
<!DOCTYPE NEWSPAPER [ <!ELEMENT NEWSPAPER (ARTICLE+)> <!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)> <!ELEMENT HEADLINE (#PCDATA)> <!ELEMENT BYLINE (#PCDATA)> <!ELEMENT LEAD (#PCDATA)> <!ELEMENT BODY (#PCDATA)> <!ELEMENT NOTES (#PCDATA)> <!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED> <!ATTLIST ARTICLE EDITOR CDATA #IMPLIED> <!ATTLIST ARTICLE DATE CDATA #IMPLIED> <!ATTLIST ARTICLE EDITION CDATA #IMPLIED> <!ENTITY NEWSPAPER "Vervet Logic Times"> <!ENTITY PUBLISHER "Vervet Logic Press"> <!ENTITY COPYRIGHT "Copyright 1998 Vervet Logic Press"> ]>
How to describe the structure of an xml document.
How to validate XML document against a DTD.
XML Schema next step.