[xsl] reg XSD generation using XSL

Subject: [xsl] reg XSD generation using XSL
From: Silent lights <silentlights@xxxxxxxxxxx>
Date: Mon, 6 Dec 2010 15:57:10 +0000 (GMT)
Hi all,
My previous posts didnt get thru the list..as I was not yet on the
mailing 
list.. hence I am posting it here. I am wondering if anyone had
attempted to 
generate an schema from an input XML document. I am kind of
stuck up in this and 
I am wondering
if could get some help here.

The Schemas
has to be ver 1.0 and generation code has to be written using XSL.

I worked
out a basic idea.. like

1) Open schema tag and check for name spaces
2) Check
if current context is root,
    2.1) is context root:- Print the element name.
2.1.1) if children and attributes are present, the context is 
complex...else
print context (single tag) as simple
            2.1.2) If complex, print
child element names based on their types
                  2.1.2.1) check if
the children are complex in a for each loop 
and then goto 2.1.1
2.2.1) close the complex tag..and check for attributes.
3) close the doc with
/schema 

I came up with a basic code and it is working for xml with one root
element and 
first children. If it goes towards grand children, I am kind of
printing out 
same element types twice or more as they are visible in previous
siblings. I am 
wondering what could be a generic solution here.

e.g. inbound
xml
<?xml version='1.0' encoding="UTF-8"?>
<catalog cat="music">
    <cd
country="RSA">
        <title>Empire Burlesque</title>
        <artist>Bob
Dylan</artist>
        <price>10.90</price>
    </cd>
    <dd county="UK">
<title1>Hide your heart 4</title1>
        <artist1>Bonnie Tyler</artist1>
<price2>9.90</price2>
    </dd>
    <dc county="UK">
        <title2>Hide your
heart 4</title2>
        <artist2>Bonnie Tyler</artist2>
<price2>9.90</price2>
    </dc>
</catalog>

The schema results in 

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
elementFormDefault="qualified"
attributeFormDefault="unqualified">
    <xs:element name="catalog">
<xs:complexType>
            <xs:sequence>
                <xs:element
name="cd" type="cdType"/>
                <xs:element name="dd"
type="ddType"/>
                <xs:element name="dc" type="dcType"/>
</xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType
name="cdType">
        <xs:sequence>
            <xs:element name="title"
type="titleType"/>
            <xs:element name="artist" type="artistType"/>
<xs:element name="price" type="priceType"/>
        </xs:sequence>
<xs:attribute name="country" type="xs:string"/>
    </xs:complexType>
<xs:simpleType name="titleType">
        <xs:restriction base="xs:string"/>
</xs:simpleType>
    <xs:simpleType name="artistType">
        <xs:restriction
base="xs:string"/>
    </xs:simpleType>
    <xs:simpleType name="priceType">
<xs:restriction base="xs:integer"/>
    </xs:simpleType>
    <xs:complexType
name="ddType">
        <xs:sequence>
            <xs:element name="title1"
type="title1Type"/>
            <xs:element name="artist1"
type="artist1Type"/>
            <xs:element name="price2" type="price2Type"/>
</xs:sequence>
        <xs:attribute name="county" type="xs:string"/>
</xs:complexType>
    <xs:simpleType name="title1Type">
<xs:restriction base="xs:string"/>
    </xs:simpleType>
    <xs:simpleType
name="artist1Type">
        <xs:restriction base="xs:string"/>
</xs:simpleType>
    <xs:simpleType name="price2Type">
        <xs:restriction
base="xs:integer"/>
    </xs:simpleType>
    <xs:complexType name="dcType">
<xs:sequence>
            <xs:element name="title2" type="title2Type"/>
<xs:element name="artist2" type="artist2Type"/>
            <xs:element
name="price2" type="price2Type"/>
        </xs:sequence>
        <xs:attribute
name="county" type="xs:string"/>
    </xs:complexType>
    <xs:simpleType
name="title2Type">
        <xs:restriction base="xs:string"/>
</xs:simpleType>
    <xs:simpleType name="artist2Type">
<xs:restriction base="xs:string"/>
    </xs:simpleType>
    <xs:simpleType
name="price2Type">
        <xs:restriction base="xs:integer"/>
</xs:simpleType>
</xs:schema>

where the simple type price2Type is repeated
twice once processing dd and then 
dc. as I am processing only the children of
that node context.  I am wondering 
what could be a generic solution here to
avoid duplicates at this context node.

thankyou
Densil

Current Thread