38

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.

0

8 Answers 8

44

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

4
  • 1
    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.
    – VilemRousi
    Commented May 20, 2013 at 8:18
  • 2
    System.Data.DataException: Undefined data type: 'token'.
    – HGMamaci
    Commented Nov 26, 2013 at 15:38
  • Doesn't work for me, neither with a very simple complex type. MsSql 2012
    – jaccso
    Commented Aug 14, 2014 at 18:31
  • 2
    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.
    – settwi
    Commented Jun 27, 2017 at 15:42
25

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.

1
  • XML Spy 2015 didn't help me at all, XSD2DB did though. Commented Sep 23, 2014 at 15:07
8

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 :)

1
6

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.

Hope it helps.

2
  • 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
  • 3
    @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.
    – Govind
    Commented Mar 17, 2015 at 6:45
5

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.

1

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.

0

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.

0

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.

2
  • 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.