Create XSD from XML instance document – new JDeveloper 11gR1 feature

3

 

While looking into the XML functionality in JDeveloper 11g, I came across a feature that was added in the 11.1.1.1.0 release – July 2009: [Create] XML Schema from XML Document. Functionality previously found in commercial products such as XMLSpy, that enables us to make a head start with the development of XML Schema Definitions by using an existing XML Document as starting point.

In this article, I will very briefly demonstrate what this functionality allows us to do. And what its current limitations are.

In short: we can indicate an XML document and have an XSD created that derives its element, type and attribute definitions from the actual XML content in that document. Most XML documents do not represent the entire set of restrictions and freedom that the XSD will describe, so the generated XSD is only a starting point – but a very useful one all the same.

At this moment, the XML source document has to exist on the file system (we cannot feed the tool with a URL). The created XSD document does not work well with multiple namespaces – as well will see in this example. The tool does not create named (complexTypes) – only (nested) elements. It does create, when so requested, simpleTypes with enumerations that describe all occurring values in the source XML document. Such enumerations are usually required only for a limited number of elements. Of course removing the types we do not need is not a lot of work. Yet it would be comfortable to specify in more detail for which elements to create these enumerations.

The example: I have created a source XML document from the RSS feed on this weblog (http://technology.amis.nl/blog/feed).I have set the default namespace for this document. A snippet from the document:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns="RSS_NS"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
	<title>AMIS Technology blog</title>
	<atom:link href="http://technology.amis.nl/blog/feed" rel="self" type="application/rss+xml" />
	<link>http://technology.amis.nl/blog</link>
	<description>Weblog for the AMIS Technology corner</description>
	<pubDate>Sat, 03 Oct 2009 07:00:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
        <item>
	  <title>Book Review: Processing XML Documents with Oracle JDeveloper 11g by Deepak Vohra</title>
	  <link>http://technology.amis.nl/blog/6295/book-review-processing-xml-documents-with-oracle-jdeveloper-11g-by-deepak-vohra</link>
	  <comments>http://technology.amis.nl/blog/6295/book-review-processing-xml-documents-with-oracle-jdeveloper-11g-by-deepak-vohra#comments</comments>
	  <pubDate>Sat, 03 Oct 2009 07:00:24 +0000</pubDate>
	  <dc:creator>Lucas Jellema</dc:creator>
	  <category><![CDATA[General]]></category>
          ...

Next I have used the XML Schema from XML Document option in the New Gallery. I selected the source XML document:

and had the XSD generated. In the visual editor, the XSD looks like this:

No named types, only elements with nested elements based on nested, anonymous complex type definitions. Well structured. With a proper global target namespace. And with strange handling of the other namespaces, especially the dc:creator element in the {http://purl.org/dc/elements/1.1/}  namespace and likewise the atom:link element in the {http://www.w3.org/2005/Atom} namespace.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="RSS_NS"
            <strong>targetNamespace=&quot;RSS_NS&quot;</strong> elementFormDefault=&quot;qualified&quot;&gt;
  &lt;xsd:element name=&quot;rss&quot;&gt;
    &lt;xsd:complexType&gt;
      &lt;xsd:sequence&gt;
        &lt;xsd:element name=&quot;channel&quot;&gt;
          &lt;xsd:complexType&gt;
            &lt;xsd:sequence&gt;
              &lt;xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/&gt;
<strong>              &lt;xsd:element name=&quot;link&quot; maxOccurs=&quot;unbounded&quot;&gt;<br /></strong>                &lt;xsd:complexType&gt;
                  &lt;xsd:simpleContent&gt;
                    &lt;xsd:extension base=&quot;xsd:string&quot;&gt;
                      &lt;xsd:attribute name=&quot;href&quot; type=&quot;xsd:string&quot;/&gt;
                      &lt;xsd:attribute name=&quot;rel&quot; type=&quot;xsd:string&quot;/&gt;
                      &lt;xsd:attribute name=&quot;type&quot; type=&quot;xsd:string&quot;/&gt;
                    &lt;/xsd:extension&gt;
                  &lt;/xsd:simpleContent&gt;
                &lt;/xsd:complexType&gt;
              &lt;/xsd:element&gt;
              &lt;xsd:element name=&quot;description&quot; type=&quot;xsd:string&quot;/&gt;
              &lt;xsd:element name=&quot;pubDate&quot; type=&quot;xsd:string&quot;/&gt;
              &lt;xsd:element name=&quot;generator&quot; type=&quot;xsd:string&quot;/&gt;
              &lt;xsd:element name=&quot;language&quot; type=&quot;xsd:string&quot;/&gt;
              &lt;xsd:element name=&quot;item&quot; maxOccurs=&quot;unbounded&quot;&gt;
                &lt;xsd:complexType&gt;
                  &lt;xsd:sequence&gt;
                    &lt;xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot;/&gt;
                    &lt;xsd:element name=&quot;link&quot; type=&quot;xsd:string&quot;/&gt;
                    &lt;xsd:element name=&quot;comments&quot; type=&quot;xsd:string&quot;/&gt;
                    &lt;xsd:element name=&quot;pubDate&quot; type=&quot;xsd:string&quot;/&gt;
<strong>                    &lt;xsd:element name=&quot;creator&quot; type=&quot;xsd:string&quot;/&gt;                     ...<br /></strong>      &lt;xsd:attribute name=&quot;wfw&quot; type=&quot;xsd:string&quot;/&gt;
<strong>      &lt;xsd:attribute name=&quot;dc&quot; type=&quot;xsd:string&quot;/&gt;<br />      &lt;xsd:attribute name=&quot;atom&quot; type=&quot;xsd:string&quot;/&gt;<br /></strong>    &lt;/xsd:complexType&gt;
  &lt;/xsd:element&gt;
&lt;/xsd:schema&gt;

This XSD is great first step – that you then have to take a little bit further. But for getting started with an XSD document, this is an excellent feature!

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

3 Comments

  1. Daniel Mouchaers on

    This is a nice feature for a free product. Nevertheless, the function acts a bit strange since it does not able to open XML-files that have a name that contains uppercase characters (on Linux anyway).

  2. …and yeah, I still strongly believe that converting relational content to XML, is like cursing in a chapel…but will create a lot of work for performance consultancy people…

  3. Lucas does it support WebDAV? I presume you used the full blown JDeveloper installation? The Java JDeveloper (smallest install base) doesn’t support XML very well (and can’t connect to a database). For those who are interested. Some people made once some attempts to create an XML Schema from relational table(s) via PL/SQL (XMLDB Realm only?). See for more info, halfway through the following thread…: http://forums.oracle.com/forums/thread.jspa?messageID=1515908