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.

Create cmd aliases in Windows

While Linux has a simple command alias, this will require more work in windows.
We will firstly use the doskey command  :

doskey ls=dir $*

This command will create an alias ls that will execute the dir command.
The variable $* allow us to pass all of the arguments entered after the alias.
We can now use ls command instead of dir. Works very well, but alias won’t be saved when we quit 🙁

We will now set multiple commands in an external file :

cmd_aliases.txt

ls=dir $*
ps=tasklist $*
clear=cls

And execute :

doskey /macrofile=c:\bin\cmd_aliases.txt

Good, but we still loose our aliases when we quit.
We’ll have to put this command in an autorun script called when cmd.exe launches.

cmd_autorun.cmd

@echo off
cls
doskey /macrofile=c:\bin\cmd_aliases.txt

And a last script to install our autorun in registry :

cmd_autorun_install.cmd

reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d c:\bin\cmd_autorun.cmd

Execute cmd_autorun_install.cmd (double click)

That’s ok !

Sources :
https://users.cs.jmu.edu/bernstdh/web/common/help/ntshell.php
http://superuser.com/questions/49170/create-an-alias-in-windows-xp/115259#115259