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:
- xmlbeans: http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html#inst2xsd
- online tool: http://www.flame-ware.com/xml2xsd/
- oxygenxml: http://www.oxygenxml.com/
(eclipse plugin: http://www.oxygenxml.com/eclipse_plugin.html) - stylus studio: http://www.stylusstudio.com/autogen_xsd.html
XSD away, Good Luck!
howto java software technology tutorials web 2.0 xmlYou can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
May 29th, 2008 at 9:36:05
exactly what I was looking for - thanks man!
May 30th, 2008 at 9:52:30
excellent!
I think you got pretty much all those XML -> XSD tools in one place - very helpful.
thank you very much!
June 3rd, 2008 at 10:25:56
“trang” is definitely good and simple to use. There is a windows tool xsd.exe
that can do that as well (if there is a need)
good article - thank you!
June 18th, 2008 at 14:06:21
trang is awesome. I had not heard about it before. I have used Oxygen before definitely the bst XML editor in the market.
Although I used Oxygen a couple of years ago and Altova used to suck back then. I have heard Altova is much better now.
September 12th, 2008 at 16:29:36
How to insert data into SQL Server 2005 using Adobe Live Cycle PDF Form….Please help me out i am new to Adobe Live Cycle…Please Post me the Code…
September 13th, 2008 at 22:48:53
Suresh,
I am not sure why you have posted you question here to the “Generate XSD from XML” article, and I have never worked with LiveCycle’s generated PDF Forms, but:
According to the LiveCycle Designer documentation:
“Designer can directly link a form to a database without scripting, as well as integrating into an enterprise data management system.”
So I would suggest to browse around configuration that is available to see how you can link your form to the DB.
For MS SQL Server specifically, you may need to use the specific driver to enable the datasource to connect to MS SQL Server successfully.
– Toly
October 24th, 2008 at 3:35:08
Is there anything available similar to this tool but reverse way , i.e. Generate XML from XSD?
October 24th, 2008 at 22:48:26
@Manisha
Firstly, you can try a couple of Eclipse’s way:
Generating XML files from XML schemas: http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.wst.xmleditor.doc.user/topics/tcrexxsd.html
Creating XML files WebTools Tutorial: http://www.eclipse.org/webtools/community/tutorials/XMLWizards/XMLWizards.html
Secondly, you may want to take a look at XMLSpear (free tool) http://www.donkeydevelopment.com/, one of the feature of which is to “Generation of complete XML documents from schema”
Finally, XMPSpy, and other XML editors can do the same thing.
Let me know if you need more info,
– Toly
November 7th, 2008 at 4:45:45
Thanks, Toly for your response.
These are all seems to be Windows based tools, I am looking for something which will work on unix, basically commandline tool. I tried Sun XML instance generator, but somehow it does not generate proper XML.
November 15th, 2008 at 5:28:29
XMLSpear is a java program and it has a XMLSpear.sh script for staring the application on Unix based systems,
-DD
November 17th, 2008 at 18:42:20
@ DD,
Thanks DD!
November 30th, 2008 at 23:13:56
Adding to the list of tools is now the OASIS CAM processor toolkit. This ingests XSD schema into a CAM template - and then allows you to create realistic XML test samples using content hinting and customized content models. Realistic test cases is the next level above random content generation. See http://wiki.oasis-open.org/cam for more information and a tutorial.
November 30th, 2008 at 23:16:02
Just realized I can also save sometime with the direct link to the XML test generator page:
http://wiki.oasis-open.org/cam/Generating_XML_test_examples_from_XSD
January 30th, 2009 at 17:43:50
Right on man this was EXACTLY what I was looking for. Thanks for putting together a list of resources and a perfect little walk through!
March 3rd, 2009 at 2:37:40
Generate XSD from XML with trang…
I found a Java-mokkula to create XSD out of XML called trang.
Features:
* Trang takes in rng, rnc, dtd, xm
* Trang can also infer a schema from one or more example XML documentsl and outputs rng, rnc, dtd and xsd.
Unfortunately current Debian and…
March 19th, 2009 at 5:15:46
Really you explained in very easy way. I did on the same on window and it woked without any problem. I used this command to create XSD file
C:\>java -jar c:\jar\trang-20030619\trang-2
0030619\trang.jar holiday-request.xml hr.xsd
Thanks.
May 1st, 2009 at 23:21:45
This message is for Suresh, although I know it’s late. I’ve worked alot with LiveCycle, and if you’re trying to get data into and out of a database, the best way we have found to do that is via web services. You can use any web services, .Net, Java, etc and link the parameters from the web service to your form. You can also create an ODBC connection directly to the database, although I recommend using web services. If you use ODBC you’re going to have to somehow get the driver installed on client machines. Web services make sure that you don’t have to touch the machine opening your form. Hope this wasn’t too late for ya pal. BTW trang is very, very cool. I use it for most of my xml to xsd conversions now. Visual Studio also has a pretty good one, but sometimes adds stuff that LiveCycle doesn’t like. Trang seems to get it right everytime…
Thanks for the post!!!
June 9th, 2009 at 2:58:05
Thanks you, I need a tools like this!!!