XML to XSD: Generating a Schema from Multiple XML Instances


I was recently talking with one of our Support Engineers about common questions he receives from customers. One that comes up a lot is schema generation, specifically, “How can I generate an XSD from XML?” The answer, of course, is to use your favorite XML editor. XMLSpy will generate a valid XSD from a DTD, a relational database structure, a JSON Schema, and of course, an XML instance.

But what many customers are surprised to learn is that the XML editor will also create an XML Schema based on a group of related XML files – and this is a common requirement. Let’s take a look at how it works.

Generate an XSD

Let’s say I have inherited a Web development project, having received a group of XML files as a starting point – but no XML Schema. To generate an XSD for those files, I first need to add them to an XMLSpy project. This will be useful afterwards since the project groups all the related files in one, easy-to-manage group of folders where I can search for specific files, assign XSLT transformations, perform batch operations such as project-wide validation and find-in-files, and so on.

It’s worth taking a moment to talk about the XMLSpy project itself. The folders may correspond to actual directories on the file system, or I can define file-type extensions for each folder so that common files are kept together in one convenient place. The project folders are virtual folders and do not need to correspond to any hierarchical organization of the files on the server itself.

I can also assign different XSL transformation parameters to each folder and have the same physical file be contained in more than one project folder. This is especially useful when I need to keep data in one XML file and use different XSL stylesheets to produce different output. And, of course, XMLSpy project management integrates with most source control systems.

OK, back to generating the XSD. The first step is to select New Project from the Project menu. XMLSpy creates a skeleton project with placeholder folders for various file types. It’s easy to add custom folders, as needed. To add the files I received to this new project, I right click on the XML Files folder and select Add Files…

Creating an XML project

…and browse to select the XML files for the Web site. This is a good time to save the project with a descriptive name, and this also saves the newly added file associations to the project.

XML Project

Now, all I need to do is right click on the XML Files folder and select Generate DTD / Schema… and review or update the configurable options.

XSD to XML functionality in XMLSpy

This will generate an XSD based on all the XML files included in the folder. After clicking OK, I’m prompted to save the auto-generated XML Schema, and it opens in the graphical XML Schema View. Here are the global elements generated based on the XML instance files:

XSD generated based on multiple XML instances

Clicking the tree icon next to any element displays the detailed content model.

Editable XSD content model

The graphical design paradigm of the XSD editor makes it easy to refine and edit this schema as needed.

That’s it – in a few quick steps I have a valid schema generated based on a group of instance files.

Which XMLSpy features help make your everyday work easier? Or, is there a time-saving feature you’d like to see added? We’d love to hear from you. Please leave a comment below!

Tags: , , ,