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 trang.zip from here (at the moment of writing “trang-20030619.zip”)

Step 2. Extract it

Use “unzip trang-version.zip”, 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="http://mycompany.com/hr/schemas">
    <holiday>
        <startDate>2006-07-03</startDate>
        <endDate>2006-07-07</endDate>
    </holiday>
    <employee>
        <number>42</number>
        <firstName>Ultimate</firstName>
        <lastName>Answer</lastName>
    </employee>
</holidayRequest>

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=”http://www.w3.org/2001/XMLSchema” elementFormDefault=”qualified” targetNamespace=”http://mycompany.com/hr/schemas” xmlns:schemas=”http://mycompany.com/hr/schemas”>
  <xs:element name=”HolidayRequest”>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref=”schemas:Holiday”/>
        <xs:element ref=”schemas:Employee”/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name=”Holiday”>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref=”schemas:StartDate”/>
        <xs:element ref=”schemas:EndDate”/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name=”StartDate” type=”xs:NMTOKEN”/>
  <xs:element name=”EndDate” type=”xs:NMTOKEN”/>
  <xs:element name=”Employee”>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref=”schemas:Number”/>
        <xs:element ref=”schemas:FirstName”/>
        <xs:element ref=”schemas:LastName”/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name=”Number” type=”xs:integer”/>
  <xs:element name=”FirstName” type=”xs:NCName”/>
  <xs:element name=”LastName” type=”xs:NCName”/>
</xs:schema>

done!

$

* – 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!

80 comments

  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(ClassLoader.java:539)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

    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.
    http://www.devdaily.com/blog/post/java/java-lang-unsupportedclassversionerror
    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=”http://marketlive.com/integration/xmlbean” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://marketlive.com/integration/xmlbean ..\..\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.
    Pankaj

  18. Is this tool supported on Windows Cmd

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