Generate xsd from xml with trang

An xsd (XML Schema definition) is a .xsd file that describes the structure of an XML document and allows us to validate and to have autocompletion on our xml file, for example with a xml editor like XMLNotePad.

An easy way do this is to generate your xsd from xml with trang command line tool.
Trang is written in java and packaged for use with a Java runtime (>=version5).

1. Download zip http://www.thaiopensource.com/download/ and extract it where you want
2. You only need to execute the following command (with your pathes)

cd C:\xml
java -jar C:\trang.jar tutorials.xml tutorials.xsd

3. Oh yeah it’s done

Our xml :

<?xml version="1.0" encoding="UTF-8"?>
<tutorials>
    <tutorial id="1" blogged="true">
        <date>2009-09-28</date>
        <author>Jonas</author>
		<copyright>Jonas Monnier</copyright>
		<title>Display JSON strings in AMFPHP 1.9 brwoser</title>
    </tutorial>
    <tutorial id="2" blogged="true">
        <date>2010-07-03</date>
        <author>Jonas</author>
		<title>Generate xsd from xml with trang</title>
    </tutorial>
</tutorials>

Our xsd :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="tutorials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="tutorial"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="tutorial">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="date"/>
        <xs:element ref="author"/>
        <xs:element minOccurs="0" ref="copyright"/>
        <xs:element ref="title"/>
      </xs:sequence>
      <xs:attribute name="blogged" use="required" type="xs:boolean"/>
      <xs:attribute name="id" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="date" type="xs:NMTOKEN"/>
  <xs:element name="author" type="xs:NCName"/>
  <xs:element name="copyright" type="xs:string"/>
  <xs:element name="title" type="xs:string"/>
</xs:schema>

We tell the XML parser that this document should be validated against a schema :

<?xml version="1.0" encoding="UTF-8"?>
<tutorials
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="tutorials.xsd">
    <tutorial id="1" blogged="true">
        <date>2009-09-28</date>
        <author>Jonas</author>
		<copyright>Jonas Monnier</copyright>
		<title>Display JSON strings in AMFPHP 1.9 brwoser</title>
    </tutorial>
    <tutorial id="2" blogged="true">
        <date>2010-07-03</date>
        <author>Jonas</author>
		<title>Generate xsd from xml with trang</title>
    </tutorial>
</tutorials>

And we change the date and author definition (nobody is perfect) :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="tutorials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="tutorial"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="tutorial">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="date"/>
        <xs:element ref="author"/>
        <xs:element minOccurs="0" ref="copyright"/>
        <xs:element ref="title"/>
      </xs:sequence>
      <xs:attribute name="blogged" use="required" type="xs:boolean"/>
      <xs:attribute name="id" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="date" type="xs:date"/>
  <xs:element name="author" type="xs:string"/>
  <xs:element name="copyright" type="xs:string"/>
  <xs:element name="title" type="xs:string"/>
</xs:schema>

That’s all !

You can create a cmd alias for an easiest use :

Windows :

doskey trang=java -jar C:\trang\trang.jar $*

See my previous post to make it working Create cmd aliases in Windows

Linux :

alias trang='java -jar ~/trang/trang.jar'

Héhé

cd C:\xml
trang tutorials.xml tutorials.xsd

More options in trang manual.

7 thoughts on “Generate xsd from xml with trang

Leave a Reply

Your email address will not be published. Required fields are marked *