Sunday, January 17, 2010

Trang: DTD to XSD conversion

This morning, I was playing with James Clark's utility which can do conversion between different XML Schema languages.

The utility is, Trang and it's described at:
http://www.thaiopensource.com/relaxng/trang.html

Here's a little example I tried with Trang (a DTD to XSD conversion).

DTD input:
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to (#PCDATA)>
  <!ELEMENT from (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body (#PCDATA)>

Here's the XSD output I got from, Trang:
  <?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="note">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="to"/>
          <xs:element ref="from"/>
          <xs:element ref="heading"/>
          <xs:element ref="body"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <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:schema>

As per Trang documentation, following combination of conversions are possible:
RELAX NG (both XML and compact syntax) & DTD
to
RELAX NG (both XML and compact syntax), DTD & XSD

I found this good, and can recommend this.

4 comments:

Javin Paul said...

Hi Mukul,

What exactly difference between XSD and DTD , I guess both are used to define XML schema.

Thanks
javin
Unix command tutorials and tips for beginners

Mukul Gandhi said...

Thanks for your comment.

You're right that both XSD and DTDs are used to define XML Schemas. It's just that there are more XML validation capabilities in XSD than DTD. Another significant difference is that, XSD is defined as XML documents (therefore can be processed by tools that can accept XML format as input) whereas DTDs are not defined as XML documents (they have a flat file grammar, which is linear). If you require some basic XML validation capabilities (like a naive element containment & ordering relationship, specifying attributes on elements, some basic data types, I think also managing "entities" and nothing else), you may still use DTDs. But for anything advanced (like XML namespaces, OO kind of data typing facilities, many more built-in types, better schema composition features and quite a few others), you may have to use XSDs (or RELAX NG perhaps, which I think differs somewhat with XSD as per the validation features available).

Franky said...

can you give tutorial how to convert it with trang?

Unknown said...
This comment has been removed by the author.