XML 2 DTD
XML 2 DTD
Definition
Internal DTD
External DTD
<?xml version="1.0"
encoding="UTF-8"?> A external DTD file
<!ELEMENT library ANY> library.dtd
<!ELEMENT book (#PCDATA)>
<!ELEMENT magazine (#PCDATA)>
• Syntax:
< !DOCTYPE Rootname [element declaration] >
Since the declarations are defined in the internal DTD subset, the XML
processor need not have to read and process external documents.
• The absence of suffix used for an operator means that the element or content
must appear exactly once.
• For example: Consider
<!ELEMENT email (TO+, FROM, CC*, Subject?, Body?)>
• The + sign in this example associated to “TO” element declares that the child element
“TO" can occur 1 or more times inside the “email" element.
• The child element “FROM" must occur once, and only once inside the “EMAIL" element.
• The * sign declares that the child element “CC" can occur 0 or more times.
• The ? sign declares that the child element “SUBJECT and BODY" can occur 0 or one
time.
• Grouping
Grou Meaning Example
p () Grouping (A|B)+
<?xml version="1.0"?>
<!DOCTYPE employee [
<!ELEMENT employee ANY>
<!ELEMENT fulltime EMPTY>
]>
<employee>James
<fulltime></fulltime> Is semantically same as <fulltime/>
</employee>
• Use #PCDATA, “|” and “*” signs in combination for a mixed content
model.
<employee supervisor=“yes”>
<name>Harry</name>
</employee>
<!ATTLIST element-name
name type requirement
name type requirement>
Value Description
Explicit value The default value of the
attribute
#REQUIRED The attribute must be
present
#IMPLIED The attribute is optional
#FIXED value The attribute value is
fixed
• In DTD
<!ATTLIST BOOK publisher CDATA “O’Reilly”>
• In XML
<BOOK>XML in a Nutshell</BOOK>
• In DTD
<!ATTLIST CONTACT phone CDATA #IMPLIED >
• In XML
<CONTACT phone=“080-4421380” />
<CONTACT />
• In DTD
<!ATTLIST PERSON pid CDATA #REQUIRED>
• In XML
<PERSON pid=“172386” />
• In DTD
<!ATTLIST COMPANY name CDATA #FIXED “ABC Inc.”>
• In XML
<COMPANY name=“ABC Inc.” />
• Characters like "<" and "&" are illegal in CDATA attribute values since
they are markup
• "<" generates an error as parser interprets it as start of a new
element
• "&" generates an error as parser interprets it as start of a
character entity
• In DTD
<!ATTLIST test
a CDATA #IMPLIED
b NMTOKEN #REQUIRED
c NMTOKENS #REQUIRED>
• In XML
<test a=">;d<1>" b="a1:12" c="3.4 div -4"/>
• In DTD
<!ELEMENT customer (name)>
<!ELEMENT name (#PCDATA)>
<!ATTLIST customer cid ID #REQUIRED>
• In XML
<customer cid=“A114">
<name>Jason</name>
</customer>
DTD XML
Parsed
Contents parsed by XML parser
Entities
• There are exactly five predefined entities: <, >, &, " ,
and '
<newspaper>&ADTEXT;</newspaper>
• 2nd form: External entities that refer to files containing non-XML data
• For example : Graphics stored in odd formats like PNG and GIF must
declare that data they contain is not XML
identifies format used in the document identifies the notation - usually with MIME-types
• For example, to include a GIF image in your XML document:
• In DTD
<!NOTATION abc SYSTEM "image/jpeg">
<!NOTATION xyz SYSTEM "image/gif">
<!ELEMENT person (#PCDATA)>
<!ATTLIST person
photo NMTOKEN #IMPLIED
filetype NOTATION (abc|xyz) #IMPLIED>
• In XML
<person photo="p1.gif" filetype=“xyz">Anny</person>