Schema by (One) Example: Part 1


Schema by (One) Example: Part 1

I know there are bunch of people out there that hate XML Schema, I’m not one of them, but I do dislike writing XML Schema. So how do you get the power without the pain? We’ll explore this with a spec by example.

One way to make XML Schema’s the easy way is to input a bunch of conformant documents into a schema generator program and it will try and deduce what the rules are and create a schema that is generally good enough for something like XML Beans but is often either too strict or too lax. In order to fix it you would either have to edit the schema by hand or create some more artificial documents that either break the restriction or pound home the rule. My take on this problem is that people fundamentally understand the structure of their documents and would like to start with that, rather than start by defining types and elements and attribute groups and all that other stuff which makes for great abstraction and reuse but doesn’t help someone build a schema at all. My test schema is the web_app_2_4.xsd schema that is used for J2EE webapps. Its quite a complicated document referencing at least 5 other schemas, but in this example I am going to simplify it a bit to get a hang of a new syntax. In later parts we can add some of the missing features and maybe fix some things that I’m sure the peanut gallery will bring up. The complete document is a bit large, so we can look at a representative part and I will attach the complete document to the entry. One of this simpler sections of the webapp looks like this:

In the part of the document defining the top level element, web-app, you have a declaration like this one:

<xsd:element name=”welcome-file-list”
 type=”j2ee:welcome-file-listType”/>

Elsewhere in the document the type used there is defined:

 <xsd:complexType name=”welcome-file-listType”>
 <xsd:sequence>
 <xsd:element name=”welcome-file”
 type=”j2ee:string”
 maxOccurs=”unbounded”>
 </xsd:element>
 </xsd:sequence>
 <xsd:attribute name=”id” type=”xsd:ID”/>
 </xsd:complexType>

My SBE tool would change this so the definition would look like:

 <welcome-file-list id=”type=xsd:ID”>
 <welcome-file>maxOccurs=unbounded,type=xsd:string</welcome-file>
 </welcome-file-list>

As you can see the element is described the same way you would use it in a document except that all the information that would normally be data is instead configuration of the attribute or for the element. This allows you to very quickly take an XML document and mark it up, ready to be transformed into an XML Schema. Is there room in the XML landscape for something like this? Do people find XML Schema intimidating and hard to use? In Part 2 I will take a look at an extension to this that allows you to further markup this document to include parsing code.
web-app_2_4.sbe
web_app_2_4.sbe