## A Conversion Tool from DTD to XML Schema

- Download
- Usage
- Samples
- How does it work?
- History

## Download

dtd2xsd.pl

Note: the documentation below doesn't reflec the changes by MH; see her announcement of 11 Jan 2001 to xml-dev.

## Usage:

>     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, STDIN will be used.

Samples:

- Simple sample: DTD -> XML Schema
- P3P DTD -> XML Schema
- SMIL DTD -> XML Schema
- XHTML Strict DTD -> XML Schema
- MathML 2.0 DTD -> XML Schema

## How does it work?

### 1. Encoding elements

DTD | XML Schema
--- | ---

    <!ELEMENT ROOT (A,B) >

    <element name="ROOT">
     <complexType content="elementOnly">
      <element ref="t:A">
      <element ref="t:B">
     </complexType>
    <element>

    <!ELEMENT ROOT (A|B) >

    <element name="ROOT">
     <complexType content="elementOnly">
      <choice>
       <element ref="t:A">
       <element ref="t:B">
      </choice>
     </complexType>
    <element>

    <!ELEMENT ROOT (A|(B,C)) >

    <element name="ROOT">
     <complexType content="elementOnly">
      <choice>
       <element ref="t:A">
       <sequence>
        <element ref="t:B">
        <element ref="t:C">
       </sequence>
      </choice>
     </complexType>
    <element>

    <!ELEMENT ROOT (A?,B+,C*) >

    <element name="ROOT">
     <complexType content="elementOnly">
      <element ref="t:A" minOccurs="0">
      <element ref="t:B" maxOccurs="unbounded">
      <element ref="t:C" minOccurs="0" maxOccurs="unbounded">
     </complexType>
    <element>

### 2. Encoding attributes

DTD | XML Schema
--- | ---

    <!ATTLIST ROOT
        a CDATA #REQUIRED>

    <element name="ROOT">
     <complexType content="elementOnly">
      <attribute name="a" type="string" use="required"/>
     </complexType>
    <element>

    <!ATTLIST ROOT
        a CDATA #IMPLIED>

    <element name="ROOT">
     <complexType content="elementOnly">
      <attribute name="a" type="string" use="optional"/>
     </complexType>
    <element>

    <!ATTLIST ROOT
        a (x|y|z) #REQUIRED;>

    <element name="ROOT">
     <complexType content="elementOnly">
      <attribute name="a">
       <simpleType base="string">
        <enumeration value="x"/>
        <enumeration value="y"/>
        <enumeration value="z"/>
       </simpleType>
      </attribute>
     </complexType>
    <element>

    <!ATTLIST ROOT
        a CDATA #FIXED "x">

    <element name="ROOT">
     <complexType content="elementOnly">
      <attribute name="a" type="string" use="fixed" value="x"/>
     </complexType>
    <element>

### 3. Simple Types, Attribute Groups, and Model Groups

Use `dtd2xsd.pl -simpleType ContentType string` to turn

    <!ENTITY % ContentType "CDATA">
        <!-- media type, as per [RFC2045] -->

into

      <simpleType name='ContentType' base='string'>
      </simpleType>

@@along with references to %ContentType;

Use `dtd2xsd.pl -simpleType Number nonNegativeInteger` to turn

    <!ENTITY % Number "CDATA">

into

      <simpleType name='Number' base='nonNegativeInteger'>
      </simpleType>

@@more on model groups, attribute groups. Meanwhile, see the makefile for examples.

## History:

April 20 2000
  - Use `use` attribute instead of `minOccurs` attribute in `<attribute>` element.
  - April 12 2000
      - Understand `#FIXED` attribute.
      - Treat external entities. Limitation: External files must be in the current directory.
      - Treat conditional sections.
    April 11 2000
      - The output is compliant with XML schema last call draft (change `<literal>`
      - to `<enumerate value='...'>`).
      - Added P3P special feature.
    April 10 2000
      - Understand such patterns as `ELEM1?`, `ELEM2*`, `ELEM3+`, `#REQUIRED`, and `#IMPLIED`, and correctly procude `minOccurs` and `maxOccurs` attributes.
      - Understand `(ELEM1|ELEM2|ELEM3)` pattern, and produce `<choice>` element.
      - Understand `(attr1|attr2|attr3)` pattern, and produce `<literal>` element.
      - Correctly understand nested bracket, and produce `<sequence>` element.
    April 03 2000
      Dan connoly modified it, so that it can produce XML schema.
    March 17 1998
      Bert Bos originally created a conversion tool from DTD to BNF.

    Yuichi Koike ($Date: 2003/05/14 06:51:19 $)