WSDL - Difference Between Element-Based and Type-Based Message Parts in WSDL

 
 

1. Introduction

Web Services Description Language (WSDL) is an XML-based language used to describe web services and explain how applications can communicate with them. It defines the operations offered by a web service, the messages exchanged between applications, the data types used in communication, and the protocols required to access the service.

In WSDL, the <message> element describes the data exchanged between a web service and its client. A message can contain one or more <part> elements, which represent individual components of the message. Each message part can be defined using either an XML Schema element or an XML Schema type.

These two approaches are known as element-based message parts and type-based message parts. Understanding their differences is important when designing structured, reusable, and interoperable web services.

2. What Are Message Parts in WSDL?

A message part is an individual component of a WSDL message. It specifies the information that must be transmitted between a client application and a web service.

For example, consider an online shopping application that provides an operation to retrieve customer information. The request message may contain a customer ID, while the response message may contain the customer's name, email address, and contact number.

WSDL uses the <message> and <part> elements to describe these data components.

A message part can be defined in two ways:

  • Using the element attribute, which references a global XML Schema element.

  • Using the type attribute, which references an XML Schema data type.

Both approaches describe message content, but they differ in how the data structure is defined and reused.

3. Understanding Element-Based Message Parts

An element-based message part uses the element attribute to reference a global element declared in an XML Schema.

An XML Schema element defines the structure and name of an XML document component. It can contain simple values, complex structures, attributes, and nested elements.

Element-based message parts are commonly used when a web service requires a clearly defined XML document structure.

Example:

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

    <types>
        <xsd:schema
            targetNamespace="http://example.com/customer">

            <xsd:element name="CustomerRequest">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="CustomerID"
                                     type="xsd:int"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

        </xsd:schema>
    </types>

    <message name="GetCustomerRequest">
        <part name="request"
              element="tns:CustomerRequest"/>
    </message>

</definitions>

In this example, the XML Schema defines an element named CustomerRequest. It contains a CustomerID field of integer type.

The WSDL message references this element through the element attribute. Therefore, the message uses the complete structure defined by the XML Schema element.

Important characteristics of element-based message parts:

  • They reference globally declared XML Schema elements.

  • They provide a clearly defined XML document structure.

  • They support complex data structures containing multiple nested elements.

  • They are useful for document-oriented web services.

  • They promote consistency when multiple services use the same XML element.

4. Understanding Type-Based Message Parts

A type-based message part uses the type attribute to reference a data type defined in XML Schema.

Instead of referencing a complete XML element, it directly identifies the data type that describes the message part.

The type can be a built-in XML Schema type, such as xsd:string, xsd:int, or xsd:boolean, or a custom-defined complex type.

Type-based message parts are useful when a service needs to define individual data values without referencing a complete XML element.

Example:

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

    <message name="GetCustomerRequest">
        <part name="CustomerID"
              type="xsd:int"/>
    </message>

</definitions>

In this example, the message contains a part named CustomerID. Its data type is defined as xsd:int, which means that the message part carries an integer value.

Unlike the element-based approach, this message part does not reference a separately declared XML element.

Important characteristics of type-based message parts:

  • They directly reference XML Schema data types.

  • They can use built-in or custom-defined data types.

  • They are suitable for describing individual values.

  • They can simplify message definitions when complex XML element structures are unnecessary.

  • They provide flexibility when defining message data.

5. Key Differences Between Element-Based and Type-Based Message Parts

Although both approaches define message content in WSDL, their implementation and usage are different.

Feature

Element-Based

Type-Based

Attribute used

element

type

References

Global XML Schema element

XML Schema data type

Structure

References a complete element declaration

References a data type

Complexity

Supports nested XML structures

Can represent simple or complex data types

Reusability

Reuses declared XML elements

Reuses declared data types

Common usage

Document-oriented services

Individual data values and type-based messages

XML structure

Element name and structure are defined in XML Schema

Element structure must be supplied by the surrounding message definition or binding

One important distinction is that a type-based message part does not automatically define a complete XML document element. It identifies the data type of the part, whereas an element-based message part references an already declared XML element.

6. Practical Example: Customer Information Service

Consider an organization that develops a customer management web service. The service allows applications to retrieve customer details using a customer identification number.

In an element-based approach, the request is represented using a global XML element named CustomerRequest. This element contains the customer identification number.

In a type-based approach, the request message directly defines a message part named CustomerID with an integer data type.

The two approaches describe similar information but organize the message differently.

For the response, an organization may define a complex XML Schema type containing the customer's name, email address, phone number, and address. This type can then be referenced by an XML Schema element or directly by a type-based message part.

Element-based definitions are particularly useful when the service needs a standardized XML document structure. Type-based definitions can be useful when the message consists of individual values or when the surrounding service definition determines the required structure.

7. Advantages of Element-Based Message Parts

Element-based message parts offer several advantages in web service development.

1. Better XML Structure: They allow developers to define complete XML structures using XML Schema elements, making messages easier to organize and understand.

2. Improved Reusability: The same global XML element can be referenced by multiple WSDL messages and operations.

3. Document-Oriented Communication: They are suitable for services that exchange structured XML documents.

4. Strong Schema Validation: XML Schema definitions help validate the structure and content of incoming and outgoing messages.

5. Better Interoperability: Clearly defined XML elements help different applications understand and process exchanged messages consistently.

8. Advantages of Type-Based Message Parts

Type-based message parts also provide useful benefits.

1. Simple Message Definitions: They allow developers to specify individual message values without creating separate global XML elements.

2. Direct Data Type Reference: Developers can directly reference built-in XML Schema data types such as strings, integers, and Boolean values.

3. Reusable Data Types: Custom XML Schema types can be reused across multiple message definitions.

4. Flexible Data Representation: They support both simple and complex data types.

5. Reduced Element Declarations: When a separate global XML element is unnecessary, developers can define message parts by referencing existing data types.

9. Limitations and Considerations

Both approaches have certain considerations that developers should understand before selecting one.

Element-based message parts require global XML Schema elements to be declared before they can be referenced. This may introduce additional schema definitions when the message contains only simple values.

Type-based message parts directly reference data types, but they do not independently provide a complete XML element declaration. Developers must ensure that the surrounding WSDL binding and message structure correctly represent the intended XML content.

Another important consideration is compatibility. Different SOAP implementations and development tools may handle message definitions differently. Therefore, developers should follow the requirements of the selected WSDL version and web service framework.

In WSDL 1.1, both element and type attributes are available for defining message parts. However, when using document-literal SOAP services, element-based message parts are generally preferred because they provide a clear XML document structure.

10. Conclusion

Element-based and type-based message parts are two important methods of defining message content in WSDL. Element-based message parts reference globally declared XML Schema elements and are particularly useful for exchanging complete, structured XML documents. Type-based message parts directly reference XML Schema data types and are useful for defining individual values or reusable data structures.

Understanding the difference between these approaches helps web service developers create properly structured messages, improve XML validation, maintain consistency, and support communication between different applications.

Both approaches contribute to the flexibility of WSDL and allow developers to select an appropriate message definition according to the requirements of their web services.