XML DTD Schema Example
An example of a very simple external XML DTD to describe the schema of a list of persons might consist of:
Taking this line by line:
people_list
is a valid element name, and an instance of such an element contains any number ofperson
elements. The*
denotes there can be 0 or moreperson
elements within thepeople_list
element.person
is a valid element name, and an instance of such an element contains one element namedname
, followed by one namedbirthdate
(optional), thengender
(also optional) andsocialsecuritynumber
(also optional). The?
indicates that an element is optional. The reference to thename
element name has no?
, so aperson
element must contain aname
element.name
is a valid element name, and an instance of such an element contains "parsed character data" (#PCDATA).birthdate
is a valid element name, and an instance of such an element contains parsed character data.gender
is a valid element name, and an instance of such an element contains parsed character data.socialsecuritynumber
is a valid element name, and an instance of such an element contains parsed character data.
An example of an XML file which makes use of and conforms to this DTD follows. The DTD is referenced here as an external subset, via the SYSTEM specifier and a URI. It assumes that we can identify the DTD with the relative URI reference "example.dtd"; the "people_list" after "!DOCTYPE" tells us that the root tags, or the first element defined in the DTD, is called "people_list":
One can render this in an XML-enabled browser (such as Internet Explorer or Mozilla Firefox) by pasting and saving the DTD component above to a text file named example.dtd and the XML file to a differently-named text file, and opening the XML file with the browser. The files should both be saved in the same directory. However, many browsers do not check that an XML document conforms to the rules in the DTD; they are only required to check that the DTD is syntactically correct. For security reasons, they may also choose not to read the external DTD.
The same DTD can also be embedded directly in the XML document itself as an internal subset, by encasing it within in the document type declaration, in which case the document no longer depends on external entities and can be processed in standalone mode:
]>Alternatives to DTDs (for specifying schemas) are available:
- XML Schema, also referred to as XML Schema Definition (XSD), has achieved Recommendation status within the W3C, and is popular for "data oriented" (that is, transactional non-publishing) XML use because of its stronger typing and easier round-tripping to Java declarations. Most of the publishing world has found that the added complexity of XSD would not bring them any particular benefits, so DTDs are still far more popular there. An XML Schema Definition is itself an XML document while a DTD is not.
- RELAX NG, which is also a part of DSDL, is an ISO international standard. It is more expressive than XSD, while providing a simpler syntax, but commercial software support has been slow in coming.
Read more about this topic: Document Type Definition