OLD BLOG. Read espenberntsen.wordpress.com instead

Introduction

In this article I will demonstrate how you can generate XSD schema from an example XML document. I will use Trang to generate a basic XSD schema compatible with the XML example document. Then I will refine the schema with namespace and an enumeration. I will use an XML file containing an order as you can see in figure 1.

<order>
	<date>2010-01-01</date>
	<id>12345</id>
	<item name="Effective Java" type="BOOK" quantity="1" />
	<item name="Spring in Action" type="BOOK" quantity="1" />
	<item name="Gladiator" type="DVD" quantity="1" />
</order>

Figure 1.

The motivation for generating the first version of the XSD schema is that it’s much easier to write an XML document than an XSD schema. Since it’s very easy to create XML documents and quite hard to write XSD schemas, you should consider generating a first version of the XSD from an XML example document. Then refine it until you’re satisfied with it. This article demonstrates how to generate the schema with Trang from inside Eclipse or with an Ant build script and how you should work with XSD schemas.

Generate XSD contract from XML example document with Trang

Download trang from here: http://code.google.com/p/jing-trang/

A simple way to generate XSD schema with Eclipse

Add the jar file to the classpath.
Right click on the project –> run as –> run configuration. Then add a new Java application. See figure 2.

Figure 2.

The required program arguments are the input XML example file and the path for the schema that Trang will generate. You separate the two arguments with a space. See figure 3.

Figure 3.

After you have executed this target, remember to update the source folder to discover the generated file. Note: I’m generating an account.xsd instead of an order.xsd on these screenshot. It’s just to replace the file paths and it you will generate an order schema instead.

Generate XSD schema with Ant

Generating XSD’s should be a part of your build scripts and the Ant target in figure 4 generates the XSD schema quite nice. All you have to specify is the Trang jar file, the input example XML file and  the path for the schema that Trang will generate.

<target name="generate-order-schema" description="Generates order schema from example XML file">
    <mkdir dir="${order-xsd-folder}" />

    <java jar="lib/trang.jar" fork="true">
        <arg value="${order-xml-folder}/order.xml" />
        <arg value="${order-xsd-folder}/order.xsd" />
    </java>

    <echo message="${order-xsd-folder}/order.xsd generated" />
</target>

Figure 4.

After you have executed this target, remember to update the folder with the generated file if you’re using Eclipse.

Tweaking the generated XSD schema

I only specified the input file and the path to the generated file when I executed Trang. This results in a very simple XSD without namespace for example. See the Trang manual for more advanced use. Figure 5 shows the generated schema.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="date"/>
        <xs:element ref="id"/>
        <xs:element maxOccurs="unbounded" ref="item"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="date" type="xs:NMTOKEN"/>
  <xs:element name="id" type="xs:integer"/>
  <xs:element name="item">
    <xs:complexType>
      <xs:attribute name="name" use="required"/>
      <xs:attribute name="quantity" use="required" type="xs:integer"/>
      <xs:attribute name="type" use="required" type="xs:NCName"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Figure 5.

The generated schema is an excellent start, but I want to tweak it manually. I want xs:string instead of xs:NMTOKEN, an enumeration for the item’s type and a namespace. After some initial tweaking, my schema looks like you can see in figure 6.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
	elementFormDefault="qualified"
	targetNamespace="http://www.redpill-linpro.com/order"
	xmlns:o="http://www.redpill-linpro.com/order">
	<xs:element name="order">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="o:date" />
				<xs:element ref="o:id" />
				<xs:element maxOccurs="unbounded" ref="o:item" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="date" type="xs:date" />
	<xs:element name="id" type="xs:integer" />
	<xs:element name="item">
		<xs:complexType>
			<xs:attribute name="name" use="required" />
			<xs:attribute name="quantity" use="required" type="xs:integer" />
			<xs:attribute name="type" use="required" type="o:itemType" />
		</xs:complexType>
	</xs:element>
	<xs:simpleType name="itemType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="BOOK" />
			<xs:enumeration value="DVD" />
			<xs:enumeration value="CD" />
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

Figure 6.

Working with XSD schema with a good IDE

With an IDE like Eclipse, it’s quite easy to create an XML document from an XSD. The XML document on figure 7 is created with Eclipse. The schema is relative to the document. You should consider an URL instead of a relative path for other situations than simple testing.

<?xml version="1.0" encoding="UTF-8"?>
<o:order xmlns:o="http://www.redpill-linpro.com/order"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.redpill-linpro.com/order order.xsd ">

  <o:date>2001-01-01</o:date>
  <o:id>0</o:id>
  <o:item name="" quantity="0" type="BOOK"/>
</o:order>

Figure 7.

An good IDE that’s aware of the schema will also give you code completion and validation support. You can see an example of code completion on figure 8.

Figure 8.

Recommendation

To generate a basic version of the schema and then incrementally improve that schema works really well. It exists several frameworks and tools that can generate the schema. I have only evaluated Trang, but it does the job well and it’s an open source project that’s very easy to use.

Test post

Hvorfor er det så lange mellomrom?

<order>
<date>2010-01-01</date>
<id>12345</id>
<item name="Effective Java" type="BOOK" quantity="1" />
<item name="Spring in Action" type="BOOK" quantity="1" />
<item name="Gladiator" type="DVD" quantity="1" />
</order>

demo demo

Veldig dårlig at det tar så lang tid å publisere da.

Post 1

Dette er en test av post 1.

for(int i = 2; i < 100; i++) {
System.out.println("number: " + i);
}

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!