XML (2) Document Type Definitions (DTD)

XML提供了和显示无关的内容数据,可是数据的结构怎么确定呢?对于复杂问题,肯定是先有一个提纲,在给出具体内容,所以我们需要一种方式来提供XML的数据结构定义,这个就是DTD。

Element Declarations
<! ELEMENT SAMPLE (#PCDATA)>


Document Type Declarations
<!DOCTYPE name_of_root_element SYSTEM "URL" [ internal DTD subset]>

external DTD subsets: Link to an outside file( often *.dtd)
internal DTD subsets: declaration in the XML file

Command-line validation
libxml2

Wed-based validation
www.cogsci.ed.ac.uk/~richard/xml-check.html
www.stg.brown.edu/service/xmlvalid

Elements Declarations
  1. Define elements, like
    <!ELEMENT SAMPLE1 ANY>

  2. Find out the #PCDATA, change all necessary elements to #PCDATA, like
    <!ELEMENT SAMPLE2 (#PCDATA)>

  3. Find out the child elements, add to necessary elements, like
    <!ELEMENT SAMPLE3 (CHILD_SAMPLE1+, CHILD_SAMPLE2?, (CHILD_SAMPLE3* | CHILD_SAMPLE4| CHILD_SAMPLE5)?,(CHILD_SAMPLE6, CHILD_SAMPLE7)*, #PCDATA )>
    Empty elements, like <!ELEMENT SAMPLE4 EMPTY>
    Comments in DTD, like <!-- comments -->

Tips:
Case sensitive
Sequence sensitive

Attribute Declarations
    <!ATTLIST Element_name Attribute_name Type Default_value>
    Attribute Types: CDATA, Emumerated, ID, IDREF, IDREFS, ENTITY, ENTINITYS, NMTOKEN, NMTOKENS, NOTION
<!ELEMENT AUTHOR EMPTY>
<!ATTLIST AUTHOR COMPANY CDATA #FIXED "TIC">
<!ATTLIST EXTENSION CDATA #IMPLIED>
<!ATTLIST AUTHOR NAME CDATA #REQUIRED>

Namespaces
    Everyone can define elements, so name collision can be avoided. Like java, to avoid the class name collision, import Name Space.

All from 《XML 1.1 Bible 3rd Edition》

posted on 2007-09-18 05:58  爱你一万年123  阅读(105)  评论(0编辑  收藏  举报

导航