XML(9) Anatomy of DTD

W3 schools reference and tutorial

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

]>



DTDs consist of three basic parts:

Element declarations

Attribute declarations

Entity declarations


Sequence

sequence must declare at the exact order:

<!ELEMENT contact (name, location, phone, knows, description)>

|  or operator:

<!ELEMENT location (address | GPS)>


Combination of both:

<!ELEMENT location (address | (latitude, longitude))>


Mixed Content with #PCDATA

text only:

<!ELEMENT first (#PCDATA)>

DTD allow mixed tags with #PCDATA

REMEMBER: when #PCDATA is mixed with other tags, then #PCDATA must be write first and follow the following format

<!ELEMENT description (#PCDATA | em | strong | br)*>

The * character is known as a cardinality indicator

If there are child elements, the * cardinality indicator must appear at the end  of the model



Cadinality

By default, elements used in the content model appear exactly once.
? the element may appear either once or not at all
+  more than once
*  zero or more

example:
<!ELEMENT name (first+, middle?, last)>
<!ELEMENT contacts (contact*)>
<!ELEMENT location (address* | (latitude, longitude))>


Empty Content

<!ELEMENT br EMPTY>


Any Content

this is not very popular
<!ELEMENT description ANY>

Attribute Declaration

Favor element to attribute.

An ATTLIST declaration consists of 
  • The ATTLIST keyword
  • The associated element's name
  • The list of declared attributes
example:
<!ELEMENT contacts (contact*)>
<!ATTLIST contacts source CDATA #IMPLIED>

ATTLIST type:

Type Description
CDATA The value is character data
(en1|en2|..) The value must be one from an enumerated list
ID The value is a unique id, the value must be #IMPLIED or #REQUIRE
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
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

The value can be one of the following
Value Explanation
value The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is not required
#FIXED value The attribute value is fixed


Entity

Entities are variables used to define shortcuts to standard text or special characters.
An entity has three parts: an ampersand (&), an entity name, and a semicolon (;).
Syntax:
internal entity
<!ENTITY entity-name "entity-value">
external entity
<!ENTITY entity-name SYSTEM "URI/URL">

DTD Example:

<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">

XML example:

<author>&writer;©right;</author>

ID, IDREF and IDREFS

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, separated by SPACE

<!ATTLIST Course Prerequisites IDREFS #IMPLIED>
<!ATTLIST Professor InstrID ID #REQUIRED>
<!ATTLIST Lecturer InstrID ID #REQUIRED>
<!ATTLIST Courseref Number IDREF #REQUIRED>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值