How can I create database tables from XSD files?

I have a set of XSDs from which I generate data access classes, stored procedures and more. What I don't have is a way to generate database table from these - is there a tool that will generate the DDL statements for me? This is not the same as Create DB table from dataset table, as I do not have dataset tables, but XSDs.

1 1 1 silver badge asked Sep 26, 2008 at 10:04 497k 101 101 gold badges 891 891 silver badges 1k 1k bronze badges

8 Answers 8

There is a command-line tool called XSD2DB, that generates database from xsd-files, available at sourceforge.

answered Apr 20, 2009 at 15:20 121k 45 45 gold badges 142 142 silver badges 158 158 bronze badges

unfortunatelly it did not work for me. It threw an error - System.Data.DataException: DataSet Object doesn´t support list and union attributes of element simpleType.

Commented May 20, 2013 at 8:18 System.Data.DataException: Undefined data type: 'token'. Commented Nov 26, 2013 at 15:38 Doesn't work for me, neither with a very simple complex type. MsSql 2012 Commented Aug 14, 2014 at 18:31

I realize this question is pretty dead, but I did some digging and found that the reason XSD2DB throws this type of exception is because it's using DataSet to interpret the XML Schema, when it should be using XmlSchemaSet or XmlSchema to compile and manipulate the schema file. The code is pretty dependent on the DataSet implementation, however, so I didn't delve into modifying it to be functional.

Commented Jun 27, 2017 at 15:42

Commercial Product: Altova's XML Spy.

Note that there's no general solution to this. An XSD can easily describe something that does not map to a relational database.

While you can try to "automate" this, your XSD's must be designed with a relational database in mind, or it won't work out well.

If the XSD's have features that don't map well you'll have to (1) design a mapping of some kind and then (2) write your own application to translate the XSD's into DDL.

Been there, done that. Work for hire -- no open source available.

answered Sep 26, 2008 at 10:29 390k 82 82 gold badges 515 515 silver badges 785 785 bronze badges XML Spy 2015 didn't help me at all, XSD2DB did though. Commented Sep 23, 2014 at 15:07

I use XSLT to do that. Write up your XSD then pass your data models through a hand written XSLT that outputs SQL commands. Writing an XSLT is way faster and reusable than a custom program /script you may write.

At least thats how I do it at work, and thanks to that I got time to hang out on SO :)

answered Sep 26, 2008 at 10:59 Robert Gould Robert Gould 69.6k 61 61 gold badges 190 190 silver badges 274 274 bronze badges Have you any sample using XSLT ? stackoverflow.com/help/how-to-answer Commented Mar 6, 2017 at 9:04

The best way to create the SQL database schema using an XSD file is a program called Altova XMLSpy, this is very simple:

  1. Create a new project
  2. On the DTDs / Schemas folder right clicking and selecting add files
  3. Selects the XSD file
  4. Open the XSD file added by double-clicking
  5. Go to the toolbar and look Conversion
  6. They select Create structure database from XML schema
  7. Selects the data source
  8. And finally give it to export the route calls immediately leave their scrip schemas with SQL Server to execute the query.
497k 101 101 gold badges 891 891 silver badges 1k 1k bronze badges answered Nov 27, 2013 at 19:32 user3043225 user3043225 61 1 1 silver badge 3 3 bronze badges

As XmlSpy 2015 Create structure database from XML schema does not seem to be an option. The only relevant option under Conversion is to create an XML schema FROM the database.

Commented Sep 23, 2014 at 14:00

@ChrisMarisic if you just open XML spy and check Convert Menu, it will show create an XML schema from database option only. What you have to do is, 1st open your xml schema file in xml spy then you can many options including Create DB structure from XML Schema. Hope this helps for others.

Commented Mar 17, 2015 at 6:45

XML Schemas describe hierarchial data models and may not map well to a relational data model. Mapping XSD's to database tables is very similar mapping objects to database tables, in fact you could use a framework like Castor that does both, it allows you to take a XML schema and generate classes, database tables, and data access code. I suppose there are now many tools that do the same thing, but there will be a learning curve and the default mappings will most like not be what you want, so you have to spend time customizing whatever tool you use.

XSLT might be the fastest way to generate exactly the code that you want. If it is a small schema hardcoding it might be faster than evaluating and learing a bunch of new technologies.

answered Nov 18, 2008 at 1:08 2,328 1 1 gold badge 16 16 silver badges 22 22 bronze badges

hyperjaxb (versions 2 and 3) actually generates hibernate mapping files and related entity objects and also does a round trip test for a given XSD and sample XML file. You can capture the log output and see the DDL statements for yourself. I had to tweak them a little bit, but it gives you a basic blue print to start with.

answered Nov 18, 2008 at 0:45

Create a Java Model using Axis wsdl2java (which can take in .xsd files).

Use a database generation tool for Java that takes in a Java Model. Surely something like Hibernate can do this? I wrote my own tool (takes a couple of days, also generates CRUD code in Java too) to save myself time at work, maybe this would be a nice personal project?

Or just do it manually so that you can check everything is correct and good! Database tools are good enough now that you can zip through creating tables for a model without too many problems.

answered Sep 26, 2008 at 10:54 17.5k 5 5 gold badges 51 51 silver badges 60 60 bronze badges

Might take a look at the XSD tool in visual studio 2k8. I have created a relational dataset from an xsd and it might help your out somehow.

answered Sep 27, 2008 at 17:23 347 1 1 silver badge 7 7 bronze badges

One can use xsd.exe to generate the schema, but you must add relationships yourself, see the XML Bulk Load Examples on MSDN, or the example in this question. (I seem to recall fixing some data types, but maybe that was generating the XSD from the XML .)

Commented Jul 4, 2013 at 11:29

As I once mentioned here the directory for xsd.exe seems to have changed on Windows 7. I have it in many Microsoft SDKs directories, so it does not seem to depend on Visual Studio anymore(?). One directory would be: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools .

Commented Jul 4, 2013 at 11:31

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.