<?xml version="1.0" encoding="utf-8"?>
<!-- 

    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
    W3C liability, trademark and document use rules apply.

    http://www.w3.org/Consortium/Legal/ipr-notice
    http://www.w3.org/Consortium/Legal/copyright-documents

    $Id: explode_examples.xsl,v 1.11 2008/03/26 15:03:02 gcowe Exp $

-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.w3.org/2002/ws/databinding/patterns/6/09/"
    xmlns:ex="http://www.w3.org/2002/ws/databinding/examples/6/09/"
    exclude-result-prefixes="">
 
    <xsl:output method="xml" name="xsd" indent="yes" encoding="UTF-8" omit-xml-declaration="no" />
    <xsl:output method="xml" name="echoxsd" indent="yes" encoding="UTF-8" omit-xml-declaration="no" />
    <xsl:output method="xml" name="wsdl" indent="yes" encoding="UTF-8" omit-xml-declaration="no" />
    <xsl:output method="xml" name="wsdl20" indent="yes" encoding="UTF-8" omit-xml-declaration="no" />
    <xsl:output method="xml" name="inst" indent="yes" encoding="UTF-8" omit-xml-declaration="no" />
    <xsl:output method="xml" name="soap11" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
    <xsl:output method="xml" name="soap12" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
    <xsl:output method="xml" name="echo" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
    
    
    <!-- generate schema for an example -->

    <xsl:param name="dir"/>
    <xsl:variable name="ns" select="'ex:'"/>
    <xsl:variable name="sep" select="'/'"/>
    
    <xsl:template match="/">
        <xsl:apply-templates select="ex:examples/ex:example"/>
    </xsl:template>
    
    <!-- Schemas and WSDLs -->
    
    <xsl:template match="ex:example">
        <xsl:param name="exampleId" select="@xml:id"/>
        <xsl:variable name="tns" select="../@targetNamespace"/>
        <xsl:variable name="wsdlns" select="../@wsdlNamespace"/>
        <xsl:variable name="exampleElementId" select="@element"/> 

        <xsl:variable name="operation" select="concat('echo',$exampleId)"/>
        <xsl:variable name="element" select="$operation"/>
        <xsl:variable name="ged1" select="concat($ns,$element)"/>
        
        <xsl:variable name="xsdFilename" select="concat( $exampleId , '.xsd' )"/>
        <xsl:variable name="echoxsdFilename" select="concat( 'echo', $exampleId , '.xsd' )"/>
        <xsl:variable name="wsdlFilename" select="concat( 'echo', $exampleId , '.wsdl' )"/>
        <xsl:variable name="wsdl20Filename" select="concat( 'echo', $exampleId , '-wsdl20.wsdl' )"/>
        <xsl:variable name="overviewFilename" select="'overview.html'"/>
        
        <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$xsdFilename}" format="xsd">
            <xsl:comment>

                Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                W3C liability, trademark and document use rules apply.
            
                http://www.w3.org/Consortium/Legal/ipr-notice
                http://www.w3.org/Consortium/Legal/copyright-documents
            
		Generated from: <xsl:value-of select="//ex:version"/><xsl:text>
</xsl:text>
            </xsl:comment><xsl:text>
</xsl:text>
		    <xsl:call-template name="types">
    	        <xsl:with-param name="echo" select="'false'"/>
    	        <xsl:with-param name="tns" select="$tns"/>
    			<xsl:with-param name="element" select="$element"/>
    			<xsl:with-param name="exampleElementId" select="$exampleElementId"/>
		    </xsl:call-template>
            
        </xsl:result-document>        

        
        <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$echoxsdFilename}" format="echoxsd">
            <xsl:comment>

                Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                W3C liability, trademark and document use rules apply.
            
                http://www.w3.org/Consortium/Legal/ipr-notice
                http://www.w3.org/Consortium/Legal/copyright-documents
            
		Generated from: <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>

            </xsl:comment><xsl:text>
</xsl:text>

		    <xsl:call-template name="types">
    	        <xsl:with-param name="echo" select="'true'"/>
    	        <xsl:with-param name="tns" select="$tns"/>
    			<xsl:with-param name="element" select="$element"/>
    			<xsl:with-param name="exampleElementId" select="$exampleElementId"/>
		    </xsl:call-template>

        </xsl:result-document>        

        
        <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$wsdlFilename}" format="wsdl">
                <xsl:comment>
                
                Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                W3C liability, trademark and document use rules apply.
                
                http://www.w3.org/Consortium/Legal/ipr-notice
                http://www.w3.org/Consortium/Legal/copyright-documents
                
		Generated from: <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>
                
            </xsl:comment><xsl:text>
</xsl:text>
            <definitions 
                xmlns="http://schemas.xmlsoap.org/wsdl/" 
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
                xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
                xmlns:def="http://www.w3.org/2002/ws/databinding/examples/6/09/"
                targetNamespace="{$wsdlns}">
                
                <types>
		    <xsl:call-template name="types">
		        <xsl:with-param name="echo" select="'true'"/>
		        <xsl:with-param name="tns" select="$tns"/>
    			<xsl:with-param name="element" select="$element"/>
    			<xsl:with-param name="exampleElementId" select="$exampleElementId"/>
		    </xsl:call-template>
                </types>
                
                <message name="{$operation}Request">
                    <part element="{$ged1}" name="{$operation}Request"/>
                </message>
                
                <message name="{$operation}Response">
                    <part element="{$ged1}" name="{$operation}Response"/>
                </message>
                
                <portType name="{$exampleId}PortType">
                    <operation name="{$operation}">
                        <input message="def:{$operation}Request"/>
                        <output message="def:{$operation}Response"/>
                    </operation>
                </portType>
                
                <binding name="SoapBinding" type="def:{$exampleId}PortType">
                    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
                    <operation name="{$operation}">
                        <soap:operation soapAction="http://www.w3.org/2002/ws/databinding/examples/6/09/#{$operation}"/>
                        <input>
                            <soap:body use="literal"/>
                        </input>
                        <output>
                            <soap:body use="literal"/>
                        </output>
                    </operation>
                </binding>
                
                <service name="{$exampleId}Service">
                    <port binding="def:SoapBinding" name="{$exampleId}Port">
                        <soap:address location="http://localhost/SoapPort"/>
                    </port>
                </service>
                
            </definitions>
        </xsl:result-document>            

        <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$wsdl20Filename}" format="wsdl20">
                <xsl:comment>
                
                Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                W3C liability, trademark and document use rules apply.
                
                http://www.w3.org/Consortium/Legal/ipr-notice
                http://www.w3.org/Consortium/Legal/copyright-documents
                
		Generated from: <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>
                
            </xsl:comment><xsl:text>
</xsl:text>
	    <description xmlns="http://www.w3.org/2006/01/wsdl"
		xmlns:ex="http://www.w3.org/2002/ws/databinding/examples/6/09/"
		xmlns:def="http://www.w3.org/2002/ws/databinding/examples/6/09/"
                targetNamespace="{$wsdlns}">

                <types>
		    <xsl:call-template name="types">
		        <xsl:with-param name="echo" select="'true'"/>
		        <xsl:with-param name="tns" select="$tns"/>
    			<xsl:with-param name="element" select="$element"/>
    			<xsl:with-param name="exampleElementId" select="$exampleElementId"/>
		    </xsl:call-template>
                </types>

	       <interface name="{$exampleId}Interface">
		  <operation name="echo{$exampleId}" pattern="http://www.w3.org/2006/01/wsdl/in-out">
		     <input element="ex:echo{$exampleId}"/>
		     <output element="ex:echo{$exampleId}"/>
		  </operation>
	       </interface>

	       <binding xmlns:wsoap="http://www.w3.org/2006/01/wsdl/soap" name="{$exampleId}SoapBinding"
		    interface="def:{$exampleId}Interface"
		    type="http://www.w3.org/2006/01/wsdl/soap"
		    wsoap:version="1.1"
		    wsoap:protocol="http://www.w3.org/2006/01/soap11/bindings/HTTP/">
		  <operation ref="def:echo{$exampleId}"
		     wsoap:soapAction="http://www.w3.org/2002/ws/databinding/examples/6/09/wsdl#echo{$exampleId}"/>
	       </binding>

	       <service name="{$exampleId}Service" interface="def:{$exampleId}Interface">
		 <endpoint name="{$exampleId}Port" binding="def:{$exampleId}SoapBinding" address="http://localhost/"/>
	      </service>
	    </description>
        </xsl:result-document>            

        <xsl:apply-templates select="ex:instance">
            <xsl:with-param name="exampleId" select="$exampleId"/>
        </xsl:apply-templates>
    </xsl:template>

<!-- Instances -->
    
    <xsl:template match="ex:instance">
        <xsl:param name="exampleId"/>
        <xsl:variable name="instance" select="@xml:id"/>
        <xsl:variable name="instanceFilename" select="concat( $exampleId, '-', $instance , '.xml' )"/>
        <xsl:variable name="echoFilename" select="concat( 'echo', $exampleId, '-', $instance , '.xml' )"/>
        <xsl:variable name="soap11Filename" select="concat( 'echo', $exampleId, '-', $instance , '-soap11.xml' )"/>
        <xsl:variable name="soap12Filename" select="concat( 'echo', $exampleId, '-', $instance , '-soap12.xml' )"/>
        <xsl:variable name="ged2" select="concat($ns,'echo', $exampleId)"/>
        
        <xsl:for-each select="*">
        
            <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$instanceFilename}" format="inst">
                <xsl:comment>
                    
                    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                    W3C liability, trademark and document use rules apply.
                    
                    http://www.w3.org/Consortium/Legal/ipr-notice
                    http://www.w3.org/Consortium/Legal/copyright-documents
                    
		    Generated from: <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>
                    
                </xsl:comment><xsl:text>
</xsl:text>
                <xsl:copy-of select="."/>
                
            </xsl:result-document>
            
            <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$echoFilename}" format="echo">
                
                <xsl:comment>
                    
                    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                    W3C liability, trademark and document use rules apply.
                    
                    http://www.w3.org/Consortium/Legal/ipr-notice
                    http://www.w3.org/Consortium/Legal/copyright-documents
                    
		    <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>
                    
                </xsl:comment><xsl:text>
</xsl:text>
                <xsl:element name="{$ged2}">
                    <xsl:copy-of select="."/>
                </xsl:element>
            </xsl:result-document>  
            
            <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$soap11Filename}" format="soap11">
                <xsl:comment>
                    
                    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                    W3C liability, trademark and document use rules apply.
                    
                    http://www.w3.org/Consortium/Legal/ipr-notice
                    http://www.w3.org/Consortium/Legal/copyright-documents
                    
		    Generated from: <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>
                    
                </xsl:comment><xsl:text>
</xsl:text>
                <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
                    <env:Header/>
                    <env:Body>
                        <xsl:element name="{$ged2}">
                            <xsl:copy-of select="."/>
                        </xsl:element>
                    </env:Body>
                </env:Envelope>
            </xsl:result-document>  
            
            <xsl:result-document href="{$dir}{$sep}{$exampleId}{$sep}{$soap12Filename}" format="soap12">
                <xsl:comment>
                    
                    Copyright (C) 2006 W3C (R) (MIT ERCIM Keio), All Rights Reserved.
                    W3C liability, trademark and document use rules apply.
                    
                    http://www.w3.org/Consortium/Legal/ipr-notice
                    http://www.w3.org/Consortium/Legal/copyright-documents
                    
		    Generated from: <xsl:value-of select="/ex:examples/ex:version"/><xsl:text>
</xsl:text>
                    
                </xsl:comment><xsl:text>
</xsl:text>
                <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
                    <env:Header/>
                    <env:Body>
                        <xsl:element name="{$ged2}">
                            <xsl:copy-of select="."/>
                        </xsl:element>
                    </env:Body>
                </env:Envelope>
            </xsl:result-document>  
        </xsl:for-each>        
    </xsl:template>


    <!-- types section for WSDL -->
    <xsl:template name="types">
        <xsl:param name="echo"/>
        <xsl:param name="tns"/>
    	<xsl:param name="element"/>
    	<xsl:param name="exampleElementId"/>

	<xsl:choose>
	    <xsl:when test="ex:typedef">
	    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="{$tns}"
		elementFormDefault="qualified"
		xmlns="http://www.w3.org/2002/ws/databinding/examples/6/09/">
		
		<xsl:copy-of select="ex:typedef/*"/>
		
		<xsl:if test="string-length($element) > 0">
		<xs:element name="{$element}">
		    <xs:complexType>
			<xs:sequence>
			    <xs:element ref="{$exampleElementId}"/>
			</xs:sequence>
		    </xs:complexType>
		</xs:element>
		</xsl:if>
	    </xs:schema>
	    </xsl:when>
	    <xsl:otherwise>
	        <xsl:for-each select="./ex:types/xs:schema">
	            <xsl:copy>
	                <xsl:copy-of select="node() | @*"/>
	                <xsl:if test="$echo='true'">
    	                <xsl:if test="string-length($element) > 0">
    	                    <xs:element name="{$element}">
    	                        <xs:complexType>
    	                            <xs:sequence>
    	                                <xs:element ref="{$exampleElementId}"/>
    	                            </xs:sequence>
    	                        </xs:complexType>
    	                    </xs:element>
    	                </xsl:if>
	                </xsl:if>
	            </xsl:copy>
	        </xsl:for-each>    
	    </xsl:otherwise>
	</xsl:choose>
    </xsl:template>

</xsl:stylesheet>
