O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Buy the book!
XML Hacks
By Michael Fitzgerald
July 2004
More Info

Create an XML Schema Document from an Instance or DTD
There are several tools that can help you generate an XML Schema document from either an instance or a DTD. This hack shows you how to get the job done with little fuss
[Discuss (0) | Link to this hack]

This hack walks you through the process of creating an XML Schema document from an XML document or a DTD. The DTD2XS utility uses a DTD, but the XSD Inference, Trang, Relaxer, and xmlspy tools all rely on an instance. Each of the following sections walks you through the simple steps necessary to get the job done with any of the tools.

LuMriX.net's DTD2XS

LuMriX.net (http://www.lumrix.net) offers a Java tool for creating an XML Schema from a DTD. Their tool is called DTD2XS (http://www.lumrix.net/dtd2xs.php). This tool comes with a command-line and a browser interface. We'll cover the command-line interface in this hack.

Download the tool and extract the archive. In the archive, you will find the files dtd2xs.class, dtd2xsd.class, xurl.class, and complextype.xsl. Copy these files to the working directory and type the following command (which assumes that the working directory is in the classpath):

java dtd2xsd time.dtd

The tool will generate the following output ().

You can redirect the XML Schema to a file using >:

java dtd2xsd time.dtd > newtime.xsd

Microsoft XSD Inference 1.0

Microsoft offers a tool that infers an XML Schema document from an instance. It's called the XSD Inference 1.0 tool. It can be run online (http://www.gotdotnet.com/xmltools/xsdinference/) and is also available for download ( http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=ed81962e-66fb-44aa-ae0c-f24b91e47228/). In an effort to be brief, I'll demonstrate only the online version.

The online version is shown in Internet Explorer in . (For a usage overview, see http://www.gotdotnet.com/xmltools/xsdinference/overview.html.) To infer a schema from an instance, click the Browse button on the form. Then search for and select time.xml from the "Choose file" dialog box. Click Open. Now click the Infer Schema button. (Click the Refine Schema button to refine a previously inferred schema. If you click Refine Schema without first clicking Infer Schema, the result will be the same as clicking Infer Schema.) The result is displayed in the "View the XML Schema(s) generated" text area (). Any errors would be displayed in the "View the error log" text area.

Figure 1. Microsoft XSD Inference tool in IE

Figure 2. Results of inferring a schema with the XSD Inference tool in IE


Assuming that Relaxer is already installed , you can type the following command to generate an XML Schema document from time.xml:

relaxer -dir:out -xsd time.xml

Because Relaxer automatically uses the filename of the input file (time.xml) as the filename for the output file (time.xsd), we use the -dir:out option so that the output file will be stored in the out subdirectory. (If a subdirectory does not exist, Relaxer creates it.) The result of running Relaxer with -xsd is the file out/time.xsd, as shown in .

Relaxer can use the content models of more than one XML document at a time, each with a different content model, in order to produce an XML Schema document. Use this command:

relaxer -dir:out -xsd time1.xml time.xml

When naming the output file, Relaxer uses the name of the first file in the list, so the output file will be out/time1.xsd, which is listed in .

Notice the difference between the content models for the atomic element in these two examples. In , the content for atomic is a minOccurs of 0 and a maxOccurs of 1. This is because atomic does not appear in time1.xml, so Relaxer interprets it as being optional rather than required.


As with a DTD, you can also generate an XML Schema document from an XML document with xmlspy (http://www.xmlspy.com). These instructions show you how to do this with xmlspy 2004 Enterprise Edition (Release 3).

  1. Start xmlspy, and with File → Open, open time.xml from the working directory where you extracted the book's file archive.

  2. You are then asked if you want to assign the generated DTD to the document. Click Yes. This adds an XML Schema instance namespace declaration and a noNamespaceSchemaLocation attribute to the XML document.

  3. Then you are asked to save the schema. Save it as spytime.xsd and click the Save button. It is okay to overwrite spytime.xsd if it already exists in the working directory.

  4. With File → Save As, save time.xml—now with a reference to the schema—as spytimexsd.xml.

Figure 3. xmlspy's Generate DTD/Schema dialog box with W3C Schema selected

Figure 4. Validating spytimexsd.xml in xmlspy (text view)

You now have in hand a variety of tools to generate schemas for instances or DTDs. At least one of them should work for you!

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.