<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<!--
// // See license.txt for license information. // // schema2xsd.xsl // // NOTES // Convert one of our schemas into XSD. // // 17-Nov-2005 phamilton Created //
-->

<xsl:stylesheet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:lit="urn:xmlliterate.schema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" exclude-result-prefixes="xsd">
    <xsl:include href="common/get_node_jscript_path.xsl"/>
    <xsl:include href="common/get_jscript_node_name.xsl"/>
    <xsl:include href="common/parent_name.xsl"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="objname"/>
    <xsl:param name="schemapath"/>
    <xsl:param name="date"/>
<!--
Include all necessary XSD's first.
-->

    <xsl:template name="objectincludes">
        <xsl:param name="object"/>
<!--
get the name of the parent class.
-->

        <xsl:variable name="superclass">
            <xsl:call-template name="parent_name">
                <xsl:with-param name="object" select="$object"/>
            </xsl:call-template>
        </xsl:variable>
<!--
make sure our superclass is included, but ony if it's not @xmlobj.
-->

        <xsl:if test="$superclass != '@xmlobj'">
            <xsd:include>
                <xsl:attribute name="schemaLocation">
                    <xsl:value-of select="$superclass"/>
                    <xsl:text>.xsd</xsl:text>
                </xsl:attribute>
            </xsd:include>
        </xsl:if>
<!--
now inlcude all members which have there own definition, but make sure we don't include ourselves.
-->

        <xsl:for-each select="$object/lit:members/*">
            <xsl:if test="count(lit:reftype) > 0 and (name(.) = 'typedobjreftype' or name(.) = 'typedobjvectortype') and lit:reftype != $object/@name">
                <xsd:include>
                    <xsl:attribute name="schemaLocation">
                        <xsl:value-of select="lit:reftype"/>
                        <xsl:text>.xsd</xsl:text>
                    </xsl:attribute>
                </xsd:include>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
<!--
Convert an object into an XSD element declaration.
-->

    <xsl:template name="object2element">
        <xsl:param name="$root"/>
        <xsl:param name="object"/>
<!--
only output an element tag if the object is not abtract.
-->

        <xsl:if test="not(starts-with($object/@name, '@'))">
            <xsd:element>
                <xsl:attribute name="name">
                    <xsl:value-of select="$object/@name"/>
                </xsl:attribute>
                <xsl:attribute name="type">
                    <xsl:value-of select="concat($object/@name, 'Type')"/>
                </xsl:attribute>
            </xsd:element>
        </xsl:if>
        <xsd:complexType>
            <xsl:attribute name="name">
                <xsl:value-of select="concat($object/@name, 'Type')"/>
            </xsl:attribute>
            <xsd:annotation>
                <xsd:documentation>
                    <xsl:value-of select="$object/lit:description"/>
                </xsd:documentation>
            </xsd:annotation>
            <xsd:complexContent>
                <xsl:variable name="superclass">
                    <xsl:call-template name="parent_name">
                        <xsl:with-param name="object" select="$object"/>
                    </xsl:call-template>
                </xsl:variable>
<!--
make sure our superclass is extended, but ony if it's not @xmlobj.
-->

                <xsl:if test="$superclass != '@xmlobj'">
                    <xsd:extension>
                        <xsl:attribute name="base">
                            <xsl:value-of select="concat($superclass, 'Type')"/>
                        </xsl:attribute>
                    </xsd:extension>
                </xsl:if>
                <xsd:sequence>
                    <xsl:for-each select="$object/lit:members/*">
                        <xsl:choose>
                            <xsl:when test="count(lit:templatetype) > 0">
                                <xsd:annotation>
                                    <xsd:documentation>
                                        <xsl:text>Ignoring template </xsl:text>
                                        <xsl:value-of select="@name"/>
                                    </xsd:documentation>
                                </xsd:annotation>
                            </xsl:when>
                            <xsl:when test="count(lit:reftype) > 0">
                                <xsl:choose>
                                    <xsl:when test="name(.) = 'typedobjreftype'">
                                        <xsd:element>
                                            <xsl:attribute name="name">
                                                <xsl:value-of select="@name"/>
                                            </xsl:attribute>
                                            <xsd:annotation>
                                                <xsd:documentation>
                                                    <xsl:value-of select="lit:description"/>
                                                </xsd:documentation>
                                            </xsd:annotation>
                                            <xsd:sequence minOccurs="0" maxOccurs="1">
                                                <xsd:element>
                                                    <xsl:attribute name="name">
                                                        <xsl:value-of select="lit:reftype"/>
                                                    </xsl:attribute>
                                                    <xsl:attribute name="type">
                                                        <xsl:value-of select="concat(lit:reftype, 'Type')"/>
                                                    </xsl:attribute>
                                                </xsd:element>
                                            </xsd:sequence>
                                        </xsd:element>
                                    </xsl:when>
                                    <xsl:when test="name(.) = 'typedobjvectortype'">
                                        <xsd:element>
                                            <xsl:attribute name="name">
                                                <xsl:value-of select="@name"/>
                                            </xsl:attribute>
                                            <xsd:annotation>
                                                <xsd:documentation>
                                                    <xsl:value-of select="lit:description"/>
                                                </xsd:documentation>
                                            </xsd:annotation>
                                            <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                                                <xsd:element>
                                                    <xsl:attribute name="name">
                                                        <xsl:value-of select="lit:reftype"/>
                                                    </xsl:attribute>
                                                    <xsl:attribute name="type">
                                                        <xsl:value-of select="concat(lit:reftype, 'Type')"/>
                                                    </xsl:attribute>
                                                </xsd:element>
                                            </xsd:sequence>
                                        </xsd:element>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsd:annotation>
                                            <xsd:documentation>
                                                <xsl:text>Ignoring reftype </xsl:text>
                                                <xsl:value-of select="@name"/>
                                            </xsd:documentation>
                                        </xsd:annotation>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:when test="count(lit:enumeration) > 0">
                                <xsd:annotation>
                                    <xsd:documentation>
                                        <xsl:text>Ignoring enumeration </xsl:text>
                                        <xsl:value-of select="@name"/>
                                    </xsd:documentation>
                                </xsd:annotation>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsd:element>
                                    <xsl:attribute name="name">
                                        <xsl:value-of select="@name"/>
                                    </xsl:attribute>
                                    <xsl:attribute name="type">
                                        <xsl:choose>
                                            <xsl:when test="name(.) = 'longtype'">
                                                <xsl:value-of select="'xs:long'"/>
                                            </xsl:when>
                                            <xsl:when test="name(.) = 'inttype'">
                                                <xsl:value-of select="'xs:integer'"/>
                                            </xsl:when>
                                            <xsl:when test="name(.) = 'doubletype'">
                                                <xsl:value-of select="'xs:decimal'"/>
                                            </xsl:when>
                                            <xsl:when test="name(.) = 'timetype'">
                                                <xsl:value-of select="'xs:time'"/>
                                            </xsl:when>
                                            <xsl:when test="name(.) = 'booltype'">
                                                <xsl:value-of select="'xs:boolean'"/>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:value-of select="'xs:string'"/>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:attribute>
                                    <xsd:annotation>
                                        <xsd:documentation>
                                            <xsl:value-of select="lit:description"/>
                                        </xsd:documentation>
                                    </xsd:annotation>
                                </xsd:element>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </xsd:sequence>
            </xsd:complexContent>
        </xsd:complexType>
    </xsl:template>
<!--
Output an object.
-->

    <xsl:template name="search_object">
        <xsl:param name="root"/>
        <xsl:param name="object"/>
        <xsl:param name="objectname"/>
        <xsl:if test="name($object) = 'object' and $object/@name = $objectname">
            <xsl:call-template name="objectincludes">
                <xsl:with-param name="object" select="$object"/>
            </xsl:call-template>
            <xsl:call-template name="object2element">
                <xsl:with-param name="root" select="$root"/>
                <xsl:with-param name="object" select="$object"/>
            </xsl:call-template>
        </xsl:if>
        <xsl:for-each select="$object/lit:derived/*">
            <xsl:call-template name="search_object">
                <xsl:with-param name="root" select="$root"/>
                <xsl:with-param name="object" select="."/>
                <xsl:with-param name="objectname" select="$objectname"/>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:if test="count($object/lit:fragment/*) > 0">
            <xsl:for-each select="$object/lit:fragment/*">
                <xsl:call-template name="search_object">
                    <xsl:with-param name="root" select="$root"/>
                    <xsl:with-param name="object" select="."/>
                    <xsl:with-param name="objectname" select="$objectname"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>
    <xsl:template match="/">
        <xsd:schema xmlns="http://www.w3schools.com" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified">
            <xsd:annotation>
                <xsd:documentation>
Generated from                     <xsl:value-of select="$schemapath"/>
through schema2xsd.xsl on                     <xsl:value-of select="$date"/>
. Edit at your own risk!                </xsd:documentation>
            </xsd:annotation>
            <xsl:call-template name="search_object">
                <xsl:with-param name="root" select="."/>
                <xsl:with-param name="object" select="lit:object"/>
                <xsl:with-param name="objectname" select="$objname"/>
            </xsl:call-template>
        </xsd:schema>
    </xsl:template>
</xsl:stylesheet>