Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DTD


May 27, 2021 XML


Table of contents


XML validation

DTD is the English Document Type Chinese meaning "document type definition."

XML with the correct syntax is called "good form" XML.

XML validated by DTD is "legitimate" XML.


A well-formed XML document

"In good form" XML documents have the correct syntax.

Grammar rules described in the previous section:

  • The XML document must have a root element
  • XML elements must have a close label
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML property values must be quoted
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Validate the XML document

Legitimate XML documents are "in good form" XML documents, which also conform to the rules of document type definition (DTD):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

In the example above, the DOCTYPE declaration is a reference to an external DTD file. The following paragraph shows the contents of this file.


XML DTD

The purpose of DTD is to define the structure of XML documents. It uses a series of legitimate elements to define the document structure:

<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

If you want to learn DTD, please find the DTD tutorial on our home page.


XML Schema

W3C supports an XML-based DTD replacement called XML Schema:

<xs:element name="note">

<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:element>

If you want to learn XML Schema, find the Schema tutorial on our home page.


A common XML validator

To help you check the syntax of XML files, we created an XML validator so that you can syntax check any XML file.

Take a look at the next chapter.

Related tutorials

DTD tutorial