The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream




    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,757 other followers

Generate XSD from XML – XSD.EXE versus on-line tools

Posted by jpluimers on 2010/11/23

Quite a while ago, I wrote about the XSD.EXE tool to generate wrapper classes from an XSD file.

Recently, I had to create an XSD based on some XML.
Actually: a client was implementing a tool, that could export some of the data as XML.
That XML had to go into their database.
But the tool vendor told the client that the underlying XSD was ‘not supported’ (odd: why allow exporting XML and then not provide something supporting as the XSD?).

Anyway, the data was not that difficult, but having an XSD at hand made the import process a lot easier.
So lets see how to get a starting XSD from an of XML files (in practice, you would do this with a couple of XML files, then collect the best pieces into your final XSD).

The XML looked a lot like this (stored as document-in-document.xml):

    <document id="a1">
      <attribute name="Name" value="1a"/>
        <document id="b1">
          <attribute name="Name" value="1b"/>
        <document id="b2">
          <attribute name="Name" value="2b"/>

So, basically it is an xml structure, containing a bunch of documents, each document can have attributes, and references to other documents.

According to the XSD.EXE documentation,  it supports generating an XSD from an XML file.
However, for this kind of XML, you immediately get an error like this:

C:\temp\xml>"C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\xsd.exe" document-in-document.xml

Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Error: There was an error processing 'document-in-document.xml'.
  - The table (document) cannot be the child table to itself in nested relations.

If you would like more help, please type "xsd /?".

The problem: XSD.EXE is aimed at .NET DataSets and those do not cope well with hierarchical data like this case.

Luckiliy, Stackoverflow got me on the right track: the first two other options to generate XSD from XML:

  1. The .NET way using the System.Xml.Schema.XmlSchemaInference.InferSchema() method (elaborated on by the Mono team)
  2. The Trang way (written in Java by James Clark of Relax NG fame)

Then How to Convert XML to Xsd Online on pointed me to these as well:

  1. Online Flame-Ware XML-2-XSD 2.0
  2. HiT Software XML utilities (registration required)
  3. XmlForAsp XML Schema Generator

The last three do a much better job of generating the XSD.

Let’s examine the differences between the two.

Since 3. does not require an XML file (just pasting the XML text suffices) it is the easiest to use.
And it generates some pretty decent starting XSD as well:

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="">
  <xsd:element name="xml" type="xmlType" />
  <xsd:complexType name="xmlType">
      <xsd:element name="documents" type="documentsType" />
  <xsd:complexType name="documentsType">
      <xsd:element name="document" type="documentType" />
  <xsd:complexType name="documentType">
      <xsd:element name="attribute" type="attributeType" />
      <xsd:element name="references" type="referencesType" />
    <xsd:attribute name="id" type="xsd:string" />
  <xsd:complexType name="referencesType">
      <xsd:element maxOccurs="unbounded" name="document" type="documentType" />
  <xsd:complexType name="documentType">
      <xsd:element name="attribute" type="attributeType" />
    <xsd:attribute name="id" type="xsd:string" />
  <xsd:complexType name="attributeType">
    <xsd:attribute name="name" type="xsd:string" />
    <xsd:attribute name="value" type="xsd:string" />
  <xsd:complexType name="attributeType">
    <xsd:attribute name="name" type="xsd:string" />
    <xsd:attribute name="value" type="xsd:string" />

So now you have a few alternatives when inferring XSD from XML.


via: Generate XSD from XML.

11 Responses to “Generate XSD from XML – XSD.EXE versus on-line tools”

  1. […] Generate XSD from XML – XSD.EXE versus on-line tools. […]

  2. […] via Generate XSD from XML – XSD.EXE versus on-line tools « The Wiert Corner – irregular stream of s…. […]

  3. […] Es gibt natürlich auch noch diverse andere Tools, u.a. auch Online-Generatoren.Eine Übersicht findet man hier. […]

  4. […] my comparison of tools for generating XSD from XML, so I used the XmlForAsp XML Schema generator, with the “Separate Complex Types” […]

  5. Ling Chia said

    Sorry, the XSD code was cut off by the forum server.

  6. Ling Chia said

    I pasted you sample XML code to, and generated the following XSD code. I’m not sure if this is what you expected.

  7. Ling Chia said

    I think you can use online XML editor to generate XSD from an XML document. It is easy. You supply an XML file, and it returns you the XSD schema.
    select the XML to XSD link.

  8. I have been using Oxygen of quite a lot lately. It also creates the XSD from your xml example just fine..

    The advantage of Oxygen for me is, that it works on Windows AND Mac ;-)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: