DTD files and XSD files?

Maybe a little XML?

When you are creating an XML file, you will need to know what elements you can use in the XML file, what attributes are allowed, which are required and which are optional etc.. This information is called an XML Schema.

One can compare an XML document to its corresponding Schema to validate whether the XML file conforms to the rules specified in the Schema.

There are two principal systems for writing schemas: DTD and XML Schema.

DTD

A DTD, or Document Type Definition is a set of rules that defines a custom markup language in XML. A DTD identifies elements and their attributes.

Including a DTD? 
A sample xml file that declares an external DTD file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE ancient_wonders 
    PUBLIC '-//kehogo//DTD WowML 2.0//EN' 'http://kehogo.com/dtd/wonders-master.dtd'>
 
    <ancient_wonder>
        <name>WonderOne</name>
    </ancient_wonder>
 
<ancient_wonders/>
Bullet points:
  • After DOCTYPE comes the root element, to which the DTD will apply. In this example our root element is ancient_wonders.
  • PUBLIC means DTD is a standardised, publicly available DTD.
  • Then comes the FPI, the Formal Public Identifier for the DTD.
  • Finally comes the actual location of the DTD file.


XML Schema

XML Schema, also known as XML Schema Definition (XSD) and XML Schema Definition Language (XSDL) is again a set of rules just like a DTD.

A sample XML Schema file may be as follows:
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
            version="3.1">
Bullet points:
  • The root element should be schema.
  • targetNamespace declares which namespace this xml schema is for. 
  • xsd here is the prefix for the namespace W3C created which contains all XML Schema elements and data types.

Including a XSD?
In order to include an external XSD file in our XML we can simply use:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
          version="3.1">
Here we are declaring that our xml file has the .../javaee namespace elements by default. xmlns:xsi declares that elements with xsi: belong to XMLSchema-instance namespace and schemaLocation tells where the xsd file actually is.