René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

XML Schema vs DTD

The XML Schema recommendation describes the content and structure of XML documents in XML. It includes the full capabilities of Document Type Definitions (DTDs) so that existing DTDs can be converted to XML Schema. XML Schemas have additional capabilities compared to DTDs.

Simple example

With DTD

<?xml version="1.0" ?>

<!DOCTYPE name 
[
  <!ELEMENT name        (title?, first_name, middle_name?, last_name, suffix?) >
  <!ELEMENT title       (#PCDATA)                                              >
  <!ELEMENT first_name  (#PCDATA)                                              >
  <!ELEMENT middle_name (#PCDATA)                                              >
  <!ELEMENT last_name   (#PCDATA)                                              >
  <!ELEMENT suffix      (#PCDATA)                                              >

  <!ENTITY  uuml        "&#252;"                                               >
]
>

<name>
  <first_name >Martin</first_name>
  <middle_name>Andrea</middle_name>
  <last_name  >M&uuml;ller</last_name>
</name>

With Schema

Note, it seems a bit hairy when it comes to defining entities with Schema .... so I left out the &uuml; of Müller here.
The Schema file:
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="uuml" type="xsd:token" fixed="ü"/>

  <xsd:complexType name="name">
    <xsd:sequence>
      <xsd:element name="title"       type="xsd:string" minOccurs="0" maxOccurs="1" />
      <xsd:element name="first_name"  type="xsd:string" minOccurs="1" maxOccurs="1" />
      <xsd:element name="middle_name" type="xsd:string" minOccurs="0" maxOccurs="1" />
      <xsd:element name="last_name"   type="xsd:string" minOccurs="1" maxOccurs="1" />
      <xsd:element name="suffix"      type="xsd:string" minOccurs="0" maxOccurs="1" />
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>
The XML file:
<?xml version="1.0" encoding="ISO-8859-1" ?>

  <name
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file://c:/www.adp-gmbh.ch/ifa/xml/person.xsd">


  <first_name >Martin</first_name>
  <middle_name>Andrea</middle_name>
  <last_name  >Mueller</last_name>

</name>