DTD
Definition
XML application
fixed set of tags for a specified application / use case
DTD
= Document type definition
-
specification / schema language
validates XML documents
lists all elements and attributes of documents
- Not in XML syntax
Limitations of DTD
- No control over exact number of children
- Limited data types
- No inheritance
- No support for namespaces
1) Element declaration
<!ELEMENT elementName
specification
>
Empty
<!ELEMENT elem
EMPTY
>
allows
<elem></elem>
or
<elem/>
No restrictions
<!ELEMENT elem
ANY
>
allows any child that was declared before
String only
<!ELEMENT name
(#PCDATA)
>
only parsed character data , no children
Fixed set of children
Only a one child per element, nothing else
parent:<!ELEMENT parent
(child)
>
child:<!ELEMENT child
(#PCDATA)
>
Only two child elements, nothing else
parent:<!ELEMENT person (first, last)>
child 1:<!ELEMENT first (#PCDATA)>
child 2:<!ELEMENT last (#PCDATA)>
Can also be used for enums
<!ELEMENT day (Mon | Tue | Wed)>
<!ELEMENT Mon (#PCDATA)>
<!ELEMENT Tue (#PCDATA)>
<!ELEMENT Wed (#PCDATA)>
Mixed content
<!ELEMENT definition
(#PCDATA | child)*
>
combination of text and elements
Setting number of children
Occurrence indicators
?
,
*
,
+
child?
zero or one
child*
zero or more
child+
one or more
Can be used for more complex expressions
Namespaces
The validator does not care about name spaces - they make no difference
Example
<!ELEMENT course (title, assessment, univ:assessment)> <!ATTLIST course xmlns CDATA #FIXED "www.oeh.ac.at"> <!ATTLIST course xmlns:univ CDATA #REQUIRED> <!ELEMENT title (#PCDATA)> <!ELEMENT assessment (#PCDATA)> <!ELEMENT univ:assessment (#PCDATA)>
<!-- Students' and University's Evaluation --> <course xmlns="www.oeh.ac.at" xmlns:univ="www.tuwien.ac.at"><title> SSD </title> <assessment> Fair </assessment > <univ:assessment> Elective </univ:assessment > </course>
2) Attribute declaration
<!ATTLIST elementName attributeName
attributeType attributeDefault
>
Attribute types
(...|...)
only one of the two given values allowed
CDATA
any string
NMTOKEN
name token = legal XML-names, that can start without letter, underscore
NMTOKENS
list of nmtokens, seperated by spaces
ID
must be unique (only used once in element)
IDREF
must be ID from other element
IDREFS
list of IDREFs, seperated by spaces
…read XML in a Nutshell, Chapter 3 for more details
Attribute defaults
#IMPLIED
optional
#REQUIRED
required
#FIXED value
default value, assigned everywhere, can’t be changed
value
default value, can be changed
3) Entity declaration
<!ENTITY
shortcut "expanded name"
>
Entity declaration
We can add more to the 5 predefined XML entities:
lt
,
gt
,
amp
,
quot
,
apos
ie.<!ENTITY ssd "Semi-structured Data">
then we use&ssd;
instead of typing it out
Declaring the DTD
Document type declaration (≠DTD)
Referencing the DTD after the XML declaration
- Using URL or local path
<!DOCTYPE person SYSTEM "http://www.mysite.com/dtds/person.dtd"> <!DOCTYPE person SYSTEM "/dtds/person.dtd"> <!DOCTYPE person SYSTEM "person.dtd"> <!--If in the same directory-->
Keyword
SYSTEM
if it is user-defined,PUBLIC
if its publically available<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
- Fully defining DTD in the same document
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE person [ <!ELEMENT person (name, tel, fax, email+)> <!ELEMENT name (first, last)> <!ELEMENT email (#PCDATA)> ]> ...
- Partially defining DTD in the same document
DTD = internal DTD subset + external DTD subset
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE person SYSTEM "person_text.dtd" [ <!ELEMENT person (name, tel, fax, email+)> <!ELEMENT name (first, last)> ]> ...
<!ELEMENT email (#PCDATA)>