From DTDs to XML Schemas (1/2) - exploring XML
From DTDs to XML Schemas
XML Schemas are the preferred way of specifying XML content models these days. Schemas are more expressive than DTDs, but how do you upgrade your self-defined DTDs? A tool provided by the W3C helps along the way.
Document Type Definitions (DTDs) describe the elements and their nesting of a specific type of XML document. XML inherited DTDs from SGML. It has become more and more apparent that some shortcomings were also inherited:
- The syntax of a DTD is different from XML, requiring the document writer to learn yet another notation, and the software to have yet another parser.
- There is no way to specify datatypes and data formats that could be used to automatically map from and to programming languages
- Neither is there a set of well-known basic elements to choose from.
To be fair these requirements were and are beyond the scope of DTDs but they need to be addressed should we hope to create a large set of XML processing tools.
The XML Schema standard was conceived to improve on DTD limitations and create a method to specify XML documents in XML, including standard pre-defined and user-specific data types.
Defining an element specifies its name and content model, meaning attributes and nested elements. In XML Schemas, the content model of elements is defined by their type. An XML document adhering to a schema can then only have elements that match the defined types. One distinguishes simple and complex types.
A number of simple types are predefined in the specification, such as string, integer and decimal. A simple type cannot contain elements or attributes in its value, whereas complex types can specify nesting of elements and associations of attributes with an element.
User-defined elements can be formed from the predefined ones using the object-oriented concepts of aggregation and inheritance. Aggregation groups a set of existing elements into a new one. Inheritance extends an already defined element so that it could stand in for the original.
The Conversion Tool
The DTD to XML Schema Conversion Tool takes a DTD and translates it into its equivalent XML schema definition. Usage of the tool is fairly straightforward:
perl dtd2xsd.pl [-alias] [-prefix p] [-ns n] [file] -alias enables special aliases (default off) -prefix t specify namespace prefix -ns http://www.w3.org/namespace/ specify namespace URI -simpletype pattern base treat parameter entities whose name match this pattern as simple datatypes derived from this base type -attrgroup pattern treat parameter entities whose name match this pattern as attribute groups -modelgroup pattern treat parameter entities whose name match this pattern as model groups
If no file is specified, standard input will be used.
Let's look at how the translation of elements and attributes works.
Produced by Michael Claßen
Created: Jun 28, 2001
Revised: Jun 28, 2001