Date Created: Fri 21-Jan-2011

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


    Using inst2xsd form XMLBeans API to generate XSD from XML

    Downloaded XMLBeans API to gain access to the inst2xsd.cmd file. Below is the help syntax

    c:\xmlbeans-2.5.0\bin>inst2xsd
    Generates XMLSchema from instance xml documents.
    Usage: inst2xsd [opts] [instance.xml]*
    Options include:
    -design [rd|ss|vb] - XMLSchema design type
    rd - Russian Doll Design - local elements and local types
    ss - Salami Slice Design - global elements and local types
    vb - Venetian Blind Design (default) - local elements and global complex types
    -simple-content-types [smart|string] - Simple content types detection (leaf text). Smart is the default
    -enumerations [never|NUMBER] - Use enumerations. Default value is 10.
    -outDir [dir] - Directory for output files. Default is '.'
    -outPrefix [file_name_prefix] - Prefix for output file names. Default is 'schema'
    -validate - Validates input instances agaist generated schemas.
    -verbose - print more informational messages
    -license - print license information
    -help - help imformation


    Example XML file:

    <?xml version="1.0"?>
    <catalog>
     
    <book id="bk101">
         
    <author>Gambardella, Matthew</author>
         
    <title>XML Developer's Guide</title>
         
    <genre>Computer</genre>
         
    <price>44.95</price>
         
    <publish_date>2000-10-01</publish_date>
         
    <description>An in-depth look at creating applications
         with XML.
    </description>
     
    </book>
     
    <book id="bk102">
         
    <author>Ralls, Kim</author>
         
    <title>Midnight Rain</title>
         
    <genre>Fantasy</genre>
         
    <price>5.95</price>
         
    <publish_date>2000-12-16</publish_date>
         
    <description>A former architect battles corporate zombies,
         an evil sorceress, and her own childhood to become queen
         of the world.
    </description>
     
    </book>
     
    <book id="bk103">
         
    <author>Corets, Eva</author>
         
    <title>Maeve Ascendant</title>
         
    <genre>Fantasy</genre>
         
    <price>5.95</price>
         
    <publish_date>2000-11-17</publish_date>
         
    <description>After the collapse of a nanotechnology
         society in England, the young survivors lay the
         foundation for a new society.
    </description>
     
    </book>
     
    <book id="bk104">
         
    <author>Corets, Eva</author>
         
    <title>Oberon's Legacy</title>
         
    <genre>Fantasy</genre>
         
    <price>5.95</price>
         
    <publish_date>2001-03-10</publish_date>
         
    <description>In post-apocalypse England, the mysterious
         agent known only as Oberon helps to create a new life
         for the inhabitants of London. Sequel to Maeve
         Ascendant.
    </description>
     
    </book>
     
    <book id="bk105">
         
    <author>Corets, Eva</author>
         
    <title>The Sundered Grail</title>
         
    <genre>Fantasy</genre>
         
    <price>5.95</price>
         
    <publish_date>2001-09-10</publish_date>
         
    <description>The two daughters of Maeve, half-sisters,
         battle one another for control of England. Sequel to
         Oberon's Legacy.
    </description>
     
    </book>
     
    <book id="bk106">
         
    <author>Randall, Cynthia</author>
         
    <title>Lover Birds</title>
         
    <genre>Romance</genre>
         
    <price>4.95</price>
         
    <publish_date>2000-09-02</publish_date>
         
    <description>When Carla meets Paul at an ornithology
         conference, tempers fly as feathers get ruffled.
    </description>
     
    </book>
     
    <book id="bk107">
         
    <author>Thurman, Paula</author>
         
    <title>Splish Splash</title>
         
    <genre>Romance</genre>
         
    <price>4.95</price>
         
    <publish_date>2000-11-02</publish_date>
         
    <description>A deep sea diver finds true love twenty
         thousand leagues beneath the sea.
    </description>
     
    </book>
     
    <book id="bk108">
         
    <author>Knorr, Stefan</author>
         
    <title>Creepy Crawlies</title>
         
    <genre>Horror</genre>
         
    <price>4.95</price>
         
    <publish_date>2000-12-06</publish_date>
         
    <description>An anthology of horror stories about roaches,
         centipedes, scorpions  and other insects.
    </description>
     
    </book>
     
    <book id="bk109">
         
    <author>Kress, Peter</author>
         
    <title>Paradox Lost</title>
         
    <genre>Science Fiction</genre>
         
    <price>6.95</price>
         
    <publish_date>2000-11-02</publish_date>
         
    <description>After an inadvertant trip through a Heisenberg
         Uncertainty Device, James Salway discovers the problems
         of being quantum.
    </description>
     
    </book>
     
    <book id="bk110">
         
    <author>O'Brien, Tim</author>
         
    <title>Microsoft .NET: The Programming Bible</title>
         
    <genre>Computer</genre>
         
    <price>36.95</price>
         
    <publish_date>2000-12-09</publish_date>
         
    <description>Microsoft's .NET initiative is explored in
         detail in this deep programmer's reference.
    </description>
     
    </book>
     
    <book id="bk111">
         
    <author>O'Brien, Tim</author>
         
    <title>MSXML3: A Comprehensive Guide</title>
         
    <genre>Computer</genre>
         
    <price>36.95</price>
         
    <publish_date>2000-12-01</publish_date>
         
    <description>The Microsoft MSXML3 parser is covered in
         detail, with attention to XML DOM interfaces, XSLT processing,
         SAX and more.
    </description>
     
    </book>
     
    <book id="bk112">
         
    <author>Galos, Mike</author>
         
    <title>Visual Studio 7: A Comprehensive Guide</title>
         
    <genre>Computer</genre>
         
    <price>49.95</price>
         
    <publish_date>2001-04-16</publish_date>
         
    <description>Microsoft Visual Studio 7 is explored in depth,
         looking at how Visual Basic, Visual C++, C#, and ASP+ are
         integrated into a comprehensive development
         environment.
    </description>
     
    </book>
    </catalog>


    Venetian Blind Design - local elements and global complex types with enumeration.

    Lets say the above code taken from a google search lives in c:/temp/books.xml and I want to generate books[x].xsd,the following command would be used.

    c:\xmlbeans-2.5.0\bin>c:\xmlbeans-2.5.0\bin\inst2xsd -outDir c:/temp -outPrefix books.xsd c:\temp\books.xml

    Result:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="catalog" type="catalogType"/>
    <xs:complexType name="bookType">
    <xs:sequence>
    <xs:element name="author">
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:enumeration value="Gambardella, Matthew"/>
    <xs:enumeration value="Ralls, Kim"/>
    <xs:enumeration value="Corets, Eva"/>
    <xs:enumeration value="Randall, Cynthia"/>
    <xs:enumeration value="Thurman, Paula"/>
    <xs:enumeration value="Knorr, Stefan"/>
    <xs:enumeration value="Kress, Peter"/>
    <xs:enumeration value="O'Brien, Tim"/>
    <xs:enumeration value="Galos, Mike"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:element>
    <xs:element type="xs:string" name="title"/>
    <xs:element name="genre">
    <xs:simpleType>
    <xs:restriction base="xs:string">
    <xs:enumeration value="Computer"/>
    <xs:enumeration value="Fantasy"/>
    <xs:enumeration value="Romance"/>
    <xs:enumeration value="Horror"/>
    <xs:enumeration value="Science Fiction"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:element>
    <xs:element name="price">
    <xs:simpleType>
    <xs:restriction base="xs:float">
    <xs:enumeration value="44.95"/>
    <xs:enumeration value="5.95"/>
    <xs:enumeration value="4.95"/>
    <xs:enumeration value="6.95"/>
    <xs:enumeration value="36.95"/>
    <xs:enumeration value="49.95"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:element>
    <xs:element type="xs:date" name="publish_date"/>
    <xs:element type="xs:string" name="description"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" use="optional"/>
    </xs:complexType>
    <xs:complexType name="catalogType">
    <xs:sequence>
    <xs:element type="bookType" name="book" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    </xs:schema>


    ===================

    if you do not want enumerations you can add the -enumerations never option also the default setting for design is to use Venetian Blind Design - local elements and global complex types

    c:\xmlbeans-2.5.0\bin>c:\xmlbeans-2.5.0\bin\inst2xsd -enumerations never -outDir
    c:/temp -outPrefix books c:\temp\books.xml

    Result:



    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="catalog" type="catalogType"/>
    <xs:complexType name="bookType">
    <xs:sequence>
    <xs:element type="xs:string" name="author"/>
    <xs:element type="xs:string" name="title"/>
    <xs:element type="xs:string" name="genre"/>
    <xs:element type="xs:float" name="price"/>
    <xs:element type="xs:date" name="publish_date"/>
    <xs:element type="xs:string" name="description"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" use="optional"/>
    </xs:complexType>
    <xs:complexType name="catalogType">
    <xs:sequence>
    <xs:element type="bookType" name="book" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    </xs:schema>

    ====================
    Russian Doll option (Russian Doll Design - local elements and local types)

    c:\xmlbeans-2.5.0\bin>c:\xmlbeans-2.5.0\bin\inst2xsd -enumerations never -design
    rd -outDir c:/temp -outPrefix books c:\temp\books.xml

    Result:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="catalog">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="book" maxOccurs="unbounded" minOccurs="0">
    <xs:complexType>
    <xs:sequence>
    <xs:element type="xs:string" name="author"/>
    <xs:element type="xs:string" name="title"/>
    <xs:element type="xs:string" name="genre"/>
    <xs:element type="xs:float" name="price"/>
    <xs:element type="xs:date" name="publish_date"/>
    <xs:element type="xs:string" name="description"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" use="optional"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    =======================

    Using Salami Slice Design - global elements and local types

    c:\xmlbeans-2.5.0\bin>c:\xmlbeans-2.5.0\bin\inst2xsd -enumerations never -design
    ss -outDir c:/temp -outPrefix books c:\temp\books.xml


    Result:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="author" type="xs:string"/>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="genre" type="xs:string"/>
    <xs:element name="price" type="xs:float"/>
    <xs:element name="publish_date" type="xs:date"/>
    <xs:element name="description" type="xs:string"/>
    <xs:element name="book">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="author"/>
    <xs:element ref="title"/>
    <xs:element ref="genre"/>
    <xs:element ref="price"/>
    <xs:element ref="publish_date"/>
    <xs:element ref="description"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" use="optional"/>
    </xs:complexType>
    </xs:element>
    <xs:element name="catalog">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="book" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories