<?xml version="1.0"?> <book> <title>XML</title> <isbn>123</isbn> </book>Can be validated against the following DTD:
<!ELEMENT book (title, isbn)> <!ELEMENT title (#PCDATA)> <!ELEMENT isbn (#PCDATA)>To declare that the XML document must conform to a DTD, the following syntax is used:
<!DOCTYPE ... >The DTD declaration must begin with "
<!DOCTYPE
", followed by a space, then the name of the root element of the XML document (example: book).<!DOCTYPE
".[
" and "]
".<?xml version="1.0"?> <!DOCTYPE book [ <!ELEMENT book (title, isbn)> <!ELEMENT title (#PCDATA)> <!ELEMENT isbn (#PCDATA)> ]> <book> <title>XML</title> <isbn>123</isbn> </book>It is also possible to reference a DTD using a System identifier or a Public identifier.
SYSTEM
" followed by the location of the external file.book.dtd
"),
or absolute by specifying a full path to a local resource (hard drive: "file:///C:/dtd/book.dtd
")
or remote resource (intranet or internet: "http://www.mtitek.com/dtd/book.dtd
").<!DOCTYPE book SYSTEM "book.dtd">
<!DOCTYPE book SYSTEM "file:///C:/dtd/book.dtd">
<!DOCTYPE book SYSTEM "http://www.mtitek.com/dtd/book.dtd">Here is the content of the "book.dtd" file:
<!ELEMENT book (title, isbn)> <!ELEMENT title (#PCDATA)> <!ELEMENT isbn (#PCDATA)>
PUBLIC
" followed by an identifier that helps locate the DTD resource in a public catalog.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">With a public identifier, it is also possible to specify a system identifier, which will be used in case the public identifier cannot be resolved:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">Note: In this case, it's not mandatory to include the keyword "SYSTEM".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" [ <!-- ... --> ]>
<!ELEMENT book (title, isbn, price)>An element declaration must start with "
<!ELEMENT
", followed by a space, then the name of the element (e.g., book), and then the element's content (placed in parentheses).EMPTY
"),
contain text ("PCDATA
"),
contain other elements,
contain a mix of elements and text,
or allow any content ("ANY
").EMPTY
")
EMPTY
".<!ELEMENT price EMPTY>In this example, the "
price
" element must be empty in the XML document.<price></price>
<price />
PCDATA
" (Parsed Character Data).<!ELEMENT title (#PCDATA)>In this example, the "
title
" element can contain only text (this includes an empty string or even a self-closing element).<title>XML</title>
<title></title>
<title />
<!ELEMENT book (title, isbn)>For example, the "
<book>
" element is allowed to contain only the following child elements: "title
", "isbn
".<!ELEMENT book (title, author)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (title, name)> <!-- OK: Both "author" and "book" use the same declaration for "title" --> <!ELEMENT title (#PCDATA)> <!-- ERROR: The "title" element must only be declared once --> <!ELEMENT name (#PCDATA)>The order of child elements in the XML document must match the order listed in the parent element declaration in the DTD (child elements are separated by commas). In the example above, the "
<book>
" element must contain two child elements ("<title>
", "<author>
")
in that exact order in the XML document.|
" character (pipe symbol) to declare that a parent element may contain only one child from a list of possible child elements.<!ELEMENT book (isbn | nbn | issn)>In the example above, the "
<book>
" element
must contain exactly one of the following: "<isbn>
", "<nbn>
", or "<issn>
".|
" means "OR" - the element can contain one of the specified options, but not multiple options simultaneously.<?xml version="1.0"?> <!DOCTYPE book [ <!ELEMENT book (title, author, (isbn | nbn | issn))> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT isbn (#PCDATA)> <!ELEMENT nbn (#PCDATA)> <!ELEMENT issn (#PCDATA)> ]> <book> <title>XML</title> <author>MTI TEK</author> <nbn>123</nbn> </book>In the example above, the "
<book>
" element must contain two child elements ("<title>
", "<author>
")
in that order in the XML document.<isbn>
", "<nbn>
", or "<issn>
",
which must appear after the "<author>
" element.PCDATA
", separating them with the "|
" character.<!ELEMENT author (#PCDATA | firstName | lastName)*>Note: The "
*
" character indicates that any combination of these elements and text is allowed.|
" character.#PCDATA
" must appear first in the list.*
" character.<?xml version="1.0"?> <!DOCTYPE book [ <!ELEMENT book (title, author)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA | firstName | lastName)*> <!ELEMENT firstName (#PCDATA)> <!ELEMENT lastName (#PCDATA)> ]> <book> <title>XML</title> <author> 1 <firstName>MTI</firstName> <lastName>TEK</lastName> 2 <lastName>XYZ</lastName> <firstName>ABC</firstName> </author> </book>Note: This syntax provides no control over the order or repetition of child elements inside the parent.
ANY
".<!ELEMENT book ANY>In this example, the "
book
" element can contain any combination of child elements and text,
regardless of their order or how many times they appear inside the "book
" element.<?xml version="1.0"?> <!DOCTYPE book [ <!ELEMENT book ANY> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA | firstName | lastName)*> <!ELEMENT firstName (#PCDATA)> <!ELEMENT lastName (#PCDATA)> ]> <book> Book information... <author> 1 <firstName>MTI</firstName> <lastName>TEK</lastName> </author> <title>XML</title> </book>Note: The "
book
" element can only contain elements that are declared in the DTD.?
": indicates that the child element can appear at most once within the parent element.+
": indicates that the child element can appear one or more times within the parent element.*
": indicates that the element can appear zero or more times within the parent element.<?xml version="1.0"?> <!DOCTYPE book [ <!ELEMENT book (title, author*, price?, info+)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT info (#PCDATA)> ]> <book> <title>XML</title> <author>MTI TEK</author> <author>ABC XYZ</author> <info>XML</info> </book>Note: Cardinality does not affect the order of elements; a child element must appear exactly in the order defined in the DTD declaration (regardless of how many times it is repeated).
<!ATTLIST book isbn CDATA #IMPLIED>An attribute declaration must start with "
<!ATTLIST
", followed by a space,
then the name of the element to which the attribute belongs (e.g., book), followed by a space,
then the name of the attribute (e.g., isbn), followed by a space,
then the attribute options (type, default value, etc.).IDREF
values.NMTOKEN
values.ENTITY
values.|
" character).<!ATTLIST book version (1 | 2 | 3 | 4) "1">In the example above, the declaration for the "
<book>
" element specifies that it may have an attribute "version
"
that can take one of the values "1", "2", "3", or "4".
The default value of the "version
" attribute is "1".|
" is used to separate the enumeration values, similar to element choice declarations.<!ATTLIST book version CDATA #FIXED "1">The attribute can be omitted, but it must not be given a value different from the fixed value.
<!ATTLIST book version CDATA #REQUIRED>
<!ATTLIST book version CDATA #IMPLIED>