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. XMLSpy will generate a valid XSD from a DTD, a relational database structure, and of course, an XML instance. But what many customers don’t know 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.

XSD editor

Let’s take a look at how it works.

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 assign XSLT transformations, perform batch operations such as project-wide validation, 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 my server.

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 schema. The first step is to select New Project from the Project menu. XMLSpy creates a skeleton project with place holder 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.

Generate XSD Dialog

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:

Schema Globals

Clicking the tree icon next to any element displays the detailed content model. 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: , , ,
2 replies
  1. Maria Amuchastegui says:

    I tried to use this feature to generate a single XSD based on 100+ XML files, but the resulting XSD had so many entities that it could not be consumed by Informatica for data mapping purposes. Is there a way to reduce the number of entities in the generated XSD? Or does the XSD have to be manually edited to eliminate duplicates?

  2. Erin Cavanaugh says:

    Thank you for your comment. Unfortunately, I'm not able to adequately answer your question without asking for some clarification, seeing a few of the files in question, looking at the generated schema, and so on. This is an issue that Altova's Support Team can respond to much better. You can access the Support Form at http://www.altova.com/support.html.

Comments are closed.