Thursday, August 5, 2010

What is a WSDL document

A Web Services Description Language (WSDL) contains detailed information about how to access a Web Service.  A WSDL contains information such as the operations, parameters and return values.  The contents of WSDL are in XML format.

More information on WSDL is found at the following resources.
WSDL 1.1 specification: http://www.w3.org/TR/wsdl
WSDL 2.0 specification: http://www.w3.org/TR/wsdl20
WSDL 2.0 primer: http://www.w3.org/TR/wsdl20-primer


WSDL Elements
WSDL document contains the following main elements:

Definitions
This is the root element of the WSDL document.  This contains a set of namespace declarations i.e. xmlns:prefix attributes.  Each namespace prefix is bound to a URI.  The name attribute names this WSDL document as AmazonSearch shown below. The targetNamespace attribute is similar to an XML Schema definition’s targetNamespace.  The names declared within this WSDL document are in the http://soap.amazon.com namespace.
A WSDL from Amazon contains the following attributes.

<wsdl:definitions xmlns:typens="http://soap.amazon.com"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    targetNamespace="http://soap.amazon.com"
    name="AmazonSearch">


Types
Data types used by the service are described by XML Schema.
An example:

<wsdl:types>
    <schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://MyCompany/MyProductService">
    <element name=”ProductName” " type="s:string" />
    <element name="Quantity" type="s:int" />
   </schema>
<wsdl:types>


A more complex example from Amazon:

<wsdl:types>
    <xsd:schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://soap.amazon.com">
    <xsd:complexType name="KeywordRequest">
        <xsd:all>
                <xsd:element name="keyword" type="xsd:string"/>
                <xsd:element name="page" type="xsd:string"/>
                <xsd:element name="mode" type="xsd:string"/>
                <xsd:element name="tag" type="xsd:string"/>
                <xsd:element name="type" type="xsd:string"/>
                <xsd:element name="devtag" type="xsd:string"/>
                <xsd:element name="sort" type="xsd:string" minOccurs="0"/>
                <xsd:element name="variations" type="xsd:string" minOccurs="0"/>
                <xsd:element name="locale" type="xsd:string" minOccurs="0"/>
            </xsd:all>
        </xsd:complexType>
        <xsd:complexType name="ProductInfo">
            <xsd:all>
                <xsd:element name="TotalResults" type="xsd:string" minOccurs="0"/>
                <!-- Total number of Search Results -->
                <xsd:element name="TotalPages" type="xsd:string" minOccurs="0"/>
                <!-- Total number of Pages of Search Results -->
                <xsd:element name="ListName" type="xsd:string" minOccurs="0"/>
                <!-- Listmania list name -->
                <xsd:element name="Details" type="typens:DetailsArray" minOccurs="0"/>
            </xsd:all>
        </xsd:complexType>
    </xsd:schema>
</wsdl:types>


Message
Defines input parameters and result of a service operation.  The part element refers to a data type.

<message name="KeywordSearchRequest">
    <part name="KeywordSearchRequest" type="typens:KeywordRequest"/>
</message>
<message name="KeywordSearchResponse">
    <part name="return" type="typens:ProductInfo"/>
</message>


Port Type
Defines the operations and messages used.

<portType name="AmazonSearchPort">
    <operation name="KeywordSearchRequest">
        <input message="typens:KeywordSearchRequest"/>
        <output message="typens:KeywordSearchResponse"/>
    </operation>
</portType>


Binding
Protocols used and message format is defined by the binding.
Note the attribute type is the portType.  The name attribute must be unique.

<binding name="AmazonSearchBinding" type="typens:AmazonSearchPort">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <!-- Binding for Amazon Web APIs - RPC, SOAP over HTTP -->
    <operation name="KeywordSearchRequest">
        <soap:operation soapAction="http://soap.amazon.com"/>
        <input>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://soap.amazon.com"/>
        </input>
        <output>
            <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://soap.amazon.com"/>
        </output>
    </operation>
</binding>


Service
Groups a set of related port elements (endpoints), each specifying a binding with a corresponding address.
The following specifies Amazon’s AmazonSearchService located at the URL address http://soap.amazon.com/onca/soap2.

<service name="AmazonSearchService">
    <!-- Endpoint for Amazon Web APIs -->
    <port name="AmazonSearchPort" binding="typens:AmazonSearchBinding">
        <soap:address location="http://soap.amazon.com/onca/soap2"/>
    </port>
</service>


Operation
Operation describes a service action.  Two main sections in a WSDL document  containing  operation information.
PortType section:

<operation name="KeywordSearchRequest">
   <input message="typens:KeywordSearchRequest"/>
   <output message="typens:KeywordSearchResponse"/>
</operation>


Binding section:

<operation name="KeywordSearchRequest">
    <soap:operation soapAction="http://soap.amazon.com"/>
    <input>
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://soap.amazon.com"/>
    </input>
    <output>
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://soap.amazon.com"/>
    </output>
</operation>


Port
Port specifies an endpoint at a particular address for a binding.  Only one address can be specified.

<!-- Endpoint for Amazon Web APIs -->
<port name="AmazonSearchPort" binding="typens:AmazonSearchBinding">
    <soap:address location="http://soap.amazon.com/onca/soap2"/>
</port>


WSDL Structure
The following defines how the above elements fit together to form a WSDL 1.1 structure.

<definitions>
    <types> </types>
    <message>
        <part> </part>
    </message>
    <portType>
        <operation>
            <input> </input>
            <output> </output>
            <fault> </fault>
        </operation>
    </portType>
    <binding>
        <operation> </operation>
    </binding>
    <service>
        <port> </port>
    </service>
<definitions>