Generate XSD from XML

There are several tools out there to create (or to infer) an XSD schema from XML document. I liked trang command line tool the most. Found it first when reading about Spring web services in Spring in Action book (very good book btw).

Here are four simple steps how to create XSD from XML* using trang:

Step 1. Get trang

Download from here (at the moment of writing “”)

Step 2. Extract it

Use “unzip”, or just winzip/winrar/7z etc.. if on windows

Step 3. Make an alias

This step is optional, but makes it extremely easy to run the tool with a single command. Make an alias to the “trang.jar” by (in my case Ubuntu/Linux) editing “~/.bashrc” and adding the following:

# execute trang.jar (create XSD from XMLs)
alias xml2xsd='java -jar ~/soft/utils/trang/trang-20030619/trang.jar'

above “~/soft/utils/trang” is the directory where “trang” was unzipped to.

Step 4. Create XSD from XML

Let’s look at the XML file we need an XSD for:

$ ls -l
total 4
-rw-r--r-- 1 user group 357 2008-05-28 15:38 holiday-request.xml
$ cat holiday-request.xml
<?xml version="1.0" encoding="UTF-8"?>
<holidayRequest xmlns="">

now run the tool against it:

$ xml2xsd holiday-request.xml hr.xsd
$ cat hr.xsd
<?xml version=”1.0″ encoding=”UTF-8″?>
<xs:schema xmlns:xs=”” elementFormDefault=”qualified” targetNamespace=”” xmlns:schemas=””>
  <xs:element name=”HolidayRequest”>
        <xs:element ref=”schemas:Holiday”/>
        <xs:element ref=”schemas:Employee”/>
  <xs:element name=”Holiday”>
        <xs:element ref=”schemas:StartDate”/>
        <xs:element ref=”schemas:EndDate”/>
  <xs:element name=”StartDate” type=”xs:NMTOKEN”/>
  <xs:element name=”EndDate” type=”xs:NMTOKEN”/>
  <xs:element name=”Employee”>
        <xs:element ref=”schemas:Number”/>
        <xs:element ref=”schemas:FirstName”/>
        <xs:element ref=”schemas:LastName”/>
  <xs:element name=”Number” type=”xs:integer”/>
  <xs:element name=”FirstName” type=”xs:NCName”/>
  <xs:element name=”LastName” type=”xs:NCName”/>



* – NOTE: “trang” can create an XSD from multiple XML documents, not just one.

List of other tools to use as an alternative to trang:

XSD away, Good Luck!


  1. Thanks very much for the detail steps!.
    trying to run it with java 1.4 and 1.5, however it gives me this error:
    Exception in thread “main” java.lang.UnsupportedClassVersionError: com/thaiopensource/relaxng/translate/Driver (Unsupported major.minor version 49.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClassInternal(

    Any idea ?
    i just follow the command:
    java -jar trang.jar example.xml example.xsd

  2. after digging, i found the answer from another forum :)
    it’s to do with system java incompactable with the version the jar file it’s compiled with.
    this works!

  3. It’s actually a great and useful piece of info. I’m glad that you just shared this useful info with us. Please stay us informed like this. Thank you for sharing.

  4. Awesome info on xsd auto gen

  5. Simple and useful, thanks ;-)

  6. Most useful, thanks

  7. I generated a schema file from test.xml, and then I validated test.xml with the schema i generated. the result is that validation failed.
    My xml file has some namespace:

  8. marketlive xmlns=”” xmlns:xsi=”” xsi:schemaLocation=” ..\..\5.8schema\MarketLive.xsd”

  9. Thanks. It worked wonderfully for me – HP UNIX platform.

  10. Evgenia Martynova

    Thanks a lot, just works as it is on my Ubuntu 11.04.

  11. Just stumble from spring training and see this is really useful. Thanks for sharing.

  12. Really useful tool. I’m going to start using it just now :) . Thank you.

  13. Really simple and great tool. Thanks for pointing to it with the detailed steps.

  14. Many thanks: in a minute I’ve got the xml schema I needed. Brilliant!

  15. Many thanks – this really helped me !!

  16. Awesome! Thank you.

  17. I tried creating XSD from test123.xml using above tool. Tool generated two XSD files test123.xsd and xsi.xsd. The xml file has schemaLocation and xmlns. The validation failed whike validating test123.xml against test123.xsd.

  18. Is this tool supported on Windows Cmd

tell me something...
  1. (required)
  2. (valid email - optional)
  3. Captcha
  4. (required)