WSDL Elements and Definitions

WSDL, which stands for Web Services Description Language, is an XML-based language used to define web services and describe how they can be accessed. It serves as a contract between the web service provider and the client by specifying what the service does, where it can be found, and how to communicate with it. In simple terms, WSDL provides all the necessary details to understand and invoke web services over the network.

What is WSDL?

WSDL (Web Services Description Language) is an XML-based format for describing network services. WSDL acts as the interface definition for a web service, explaining what functions are available, what input is required, and what the output will be. Think of it as a contract between the service provider and the consumer, outlining how the client can communicate with the service.

In simpler terms, WSDL specifies:

  • What the service can do.
  • How to call the service.
  • Where the service is located.

The Importance of WSDL in Web Services

WSDL is essential in service-oriented architecture (SOA) because it provides a standardized way for different systems to communicate over the internet. Key benefits include:

  • Interoperability: WSDL enables different systems, platforms, and programming languages to communicate with each other.
  • Automation: Many development tools can auto-generate code or configuration files based on the WSDL document, saving developers time.
  • Clarity: WSDL makes the contract between the client and service explicit, so there’s no ambiguity in terms of inputs, outputs, or errors.

How WSDL Works

WSDL serves as a communication protocol for defining the services that are available over the network. Here’s how it works in a simplified manner:

  1. Service provider creates a WSDL file that describes the available service, including its methods, parameters, and return values.
  2. Client application reads the WSDL file to understand how to interact with the service.
  3. SOAP messages or HTTP requests are then generated based on the information provided in the WSDL, and the client interacts with the service.

WSDL Document Structure

A WSDL document is made up of several key sections, all enclosed within the root <definitions> element. These sections define the data types used, the operations offered by the service, and how to communicate with the service. The basic structure of a WSDL file looks like this:

				
					<definitions>
   
   <types>...</types>

   
   <message>...</message>

   
   <portType>...</portType>

   
   <binding>...</binding>

   
   <service>...</service>
</definitions>

				
			

Key Elements of WSDL

The WSDL document is composed of six key elements:

1. Definitions

The root element in the WSDL file, <definitions>, contains all the WSDL data. It defines the name of the service and serves as a container for all other elements.

Example:

				
					<definitions name="WeatherService"
   targetNamespace="http://www.example.com/weather"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

				
			

Explanation:

  • name: Name of the service being defined.
  • targetNamespace: A unique namespace that helps identify the service.

2. Types

The <types> element is used to define the data types (such as integers, strings, and custom complex types) that are exchanged between the client and the web service. WSDL uses XML Schema to define these data types.

Example:

				
					<definitions name="WeatherService"
   targetNamespace="http://www.example.com/weather"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
				
			

Explanation:

  • TemperatureRequest: A request that expects a string (city name).
  • TemperatureResponse: The response will be a float (temperature value).

3. Message

The <message> element defines the data that will be passed to the service and received as output. Each <message> element can contain one or more <part> sub-elements.

Example:

				
					<message name="GetTemperatureRequest">
   <part name="cityName" element="xsd:string"/>
</message>

<message name="GetTemperatureResponse">
   <part name="temperature" element="xsd:float"/>
</message>
				
			

Explanation:

  • GetTemperatureRequest: This message contains a single part, which is the city name.
  • GetTemperatureResponse: This message contains the temperature result.

4. PortType

The <portType> element defines a group of related operations that the service offers. Each operation corresponds to a method in the web service.

Example:

				
					<portType name="WeatherPortType">
   <operation name="GetTemperature">
      <input message="tns:GetTemperatureRequest"/>
      <output message="tns:GetTemperatureResponse"/>
   </operation>
</portType>

				
			

Explanation:

  • GetTemperature: This operation takes a GetTemperatureRequest and returns a GetTemperatureResponse.

5. Binding

The <binding> element specifies how the operations defined in the <portType> will be transmitted over the network. It maps the operations to a specific protocol, like SOAP or HTTP.

Example:

				
					<binding name="WeatherServiceBinding" type="tns:WeatherPortType">
   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
   <operation name="GetTemperature">
      <soap:operation soapAction="http://www.example.com/GetTemperature"/>
      <input>
         <soap:body use="literal"/>
      </input>
      <output>
         <soap:body use="literal"/>
      </output>
   </operation>
</binding>

				
			

Explanation:

  • soap : Indicates that SOAP is the protocol used.
  • style=”rpc”: Means the service will use Remote Procedure Call (RPC) style communication.

6. Service

The <service> element groups all the operations and specifies the endpoint where the service can be accessed. It contains one or more <port> elements.

Example:

				
					<service name="WeatherService">
   <port name="WeatherPort" binding="tns:WeatherServiceBinding">
      <soap:address location="http://www.example.com/weatherService"/>
   </port>
</service>

				
			

Explanation:

  • location: The URL where the service is available.

Detailed Explanation of WSDL Elements with Examples

Now, let’s dive deeper into each element with a more detailed explanation.

1. definitions Element

This is the root element of the WSDL document and holds all the necessary information about the service. It defines namespaces to avoid conflicts and uniquely identifies the service.

Example:

				
					<definitions name="MathService" 
             targetNamespace="http://www.example.com/math"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

				
			
  • name: The name of the service is MathService.
  • targetNamespace: The unique namespace for this service is http://www.example.com/math.
  • xmlns: Declares the namespaces used in the document.

2. types Element

The <types> element defines the data types that the service uses. This could include custom data types (complex types) that need to be serialized and sent over the network.

Example:

				
					<types>
   <xsd:schema>
      <xsd:element name="AddRequest" type="xsd:int"/>
      <xsd:element name="AddResponse" type="xsd:int"/>
   </xsd:schema>
</types>

				
			

This WSDL defines two elements:

  • AddRequest: An integer input type.
  • AddResponse: An integer output type.

3. message Element

The <message> element defines the structure of the messages that will be exchanged between the client and server.

Example:

				
					<message name="AddRequest">
   <part name="number1" type="xsd:int"/>
   <part name="number2" type="xsd:int"/>
</message>

<message name="AddResponse">
   <part name="result" type="xsd:int"/>
</message>

				
			

This example defines:

  • AddRequest: A message with two parts, number1 and number2, both of which are integers.
  • AddResponse: A message that contains a single part, result, which is an integer.

4. portType Element

The <portType> element groups related operations (methods) and is analogous to an interface in object-oriented programming.

Example:

				
					<portType name="MathPortType">
   <operation name="Add">
      <input message="tns:AddRequest"/>
      <output message="tns:AddResponse"/>
   </operation>
</portType>

				
			

This WSDL defines an operation called Add that takes an AddRequest and returns an AddResponse.

5. binding Element

The <binding> element specifies how to call the service operations. It includes details about the transport protocol (e.g., SOAP, HTTP) and the encoding style.

Example:

				
					<binding name="MathServiceBinding" type="tns:MathPortType">
   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
   <operation name="Add">
      <soap:operation soapAction="http://www.example.com/add"/>
      <input>
         <soap:body use="literal"/>
      </input>
      <output>
         <soap:body use="literal"/>
      </output>
   </operation>
</binding>

				
			
  • soap : Specifies that SOAP is used as the transport protocol.
  • operation name=”Add”: Describes how the Add operation will be invoked.

6. service Element

The <service> element provides the network address where the web service can be accessed.

Example:

				
					<service name="MathService">
   <port name="MathPort" binding="tns:MathServiceBinding">
      <soap:address location="http://www.example.com/mathService"/>
   </port>
</service>

				
			

This example defines the MathService, which can be accessed at http://www.example.com/mathService.

Example WSDL Document

Here’s a full example of a WSDL document that describes a simple web service for adding two numbers:

				
					<definitions name="MathService"
             targetNamespace="http://www.example.com/math"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:tns="http://www.example.com/math">

   <types>
      <xsd:schema>
         <xsd:element name="AddRequest" type="xsd:int"/>
         <xsd:element name="AddResponse" type="xsd:int"/>
      </xsd:schema>
   </types>

   <message name="AddRequest">
      <part name="number1" type="xsd:int"/>
      <part name="number2" type="xsd:int"/>
   </message>

   <message name="AddResponse">
      <part name="result" type="xsd:int"/>
   </message>

   <portType name="MathPortType">
      <operation name="Add">
         <input message="tns:AddRequest"/>
         <output message="tns:AddResponse"/>
      </operation>
   </portType>

   <binding name="MathServiceBinding" type="tns:MathPortType">
      <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="Add">
         <soap:operation soapAction="http://www.example.com/add"/>
         <input>
            <soap:body use="literal"/>
         </input>
         <output>
            <soap:body use="literal"/>
         </output>
      </operation>
   </binding>

   <service name="MathService">
      <port name="MathPort" binding="tns:MathServiceBinding">
         <soap:address location="http://www.example.com/mathService"/>
      </port>
   </service>

</definitions>

				
			

Advanced Concepts in WSDL

1.Extending WSDL

WSDL can be extended by adding more operations or additional message types. Extensions allow for custom functionality and more complex services.

2. Custom Data Types

While WSDL primarily uses XML Schema data types, you can define custom types for complex data structures like arrays, objects, and enumerations.

3. WSDL 1.1 vs WSDL 2.0

WSDL 1.1 is the most widely used version, but WSDL 2.0 introduces improvements such as better support for HTTP binding and asynchronous communication.

WSDL is a powerful XML-based standard for describing web services, making it an essential part of Service-Oriented Architecture (SOA). Understanding WSDL allows developers to define web services that can be consumed by clients regardless of platform or language. By mastering the various WSDL elements, including types, message, portType, binding, and service, developers can ensure that their services are robust, interoperable, and easy to integrate. Happy Coding!❤️

Table of Contents