How to generate XSD from XML

In our day to day software development process we often come across the situations wherein we have been given some XML files and asked to convert them into XSD’s.

What is XSD?
XSD can be used to express a set of rules to which an XML document must conform in order to be considered 'valid' according to that schema. However, unlike most other schema languages, XSD was also designed with the intent that determination of a document's validity would produce a collection of information adhering to specific data types

This post step by step explains how to create a XSD file using existing XML file. We will use a tool called “Trang” for achieving this.

1) Go to Trang’s project's download page and download the latest version. At the current time, the file is the latest.

2) Extract the ZIP archive and inside you will find a JAR file called trang.jar. Contents of the zip file will be as below.

3) Copy the trang.jar file to your project where you have your XML file.

Contents of my Employee.xml is as below:

4) Open the command prompt and then go to your XML folder. Assuming you have installed java,
Then type java –jar trang.jar Employee.xml Employee.xsd

Take a brief look at the generated Employee.xsd and replace any very specific element types (in this case thexs:NCName) to more simpler types, such as xs:string.

Very simple isn’t it?