Web 服務描述語言(Web Services Description Language,WSDL)提供了一種描述 Web 服務(大多使用 SOAP)的簡單方法。WSDL 允許您描述利用 SOAP 標准所提供的服務和接口。
比方說,可以創建描述某台服務器上提供的服務的 WSDL 文件,然後把該文件分發給需要這些服務的 Web 服務消費者。通過閱讀和解析 WSDL 文件,消費者能夠了解到使用這些 Web 服務需要知道的所有信息,包括可以交換的數據類型、參數以及返回的各種錯誤和其他信息。
再次使用來自 W3C 的例子,可以看到不同遠程函數的聲明和交換的數據都是通過結構的 XML 定義處理的,如清單 3 所示。
清單 3. 不同遠程函數和交換數據的 XML 定義
<?xml version="1.0"?>
<!-- root element wsdl:definitions defines set of related services -->
<wsdl:definitions name="EndorsementSearch"
targetNamespace="http://namespaces.snowboard-info.com"
xmlns:es="http://www.snowboard-info.com/EndorsementSearch.wsdl"
xmlns:esxsd="http://schemas.snowboard-info.com/EndorsementSearch.xsd"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<!-- wsdl:types encapsulates schema definitions of communication types;
here using xsd -->
<wsdl:types>
<!-- all type declarations are in a chunk of xsd -->
<xsd:schema targetNamespace="http://namespaces.snowboard-info.com"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<!-- xsd definition: GetEndorsingBoarder [manufacturer string,
model string] -->
<xsd:element name="GetEndorsingBoarder">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="manufacturer" type="string"/>
<xsd:element name="model" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!-- xsd definition: GetEndorsingBoarderResponse
[... endorsingBoarder string ...] -->
<xsd:element name="GetEndorsingBoarderResponse">
<xsd:complexType>
<xsd:all>
<xsd:element name="endorsingBoarder" type="string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
<!-- xsd definition: GetEndorsingBoarderFault
[... errorMessage string ...] -->
<xsd:element name="GetEndorsingBoarderFault">
<xsd:complexType>
<xsd:all>
<xsd:element name="errorMessage" type="string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<!-- wsdl:message elements describe potential transactions -->
<!-- request GetEndorsingBoarderRequest is of type GetEndorsingBoarder -->
<wsdl:message name="GetEndorsingBoarderRequest">
<wsdl:part name="body" element="esxsd:GetEndorsingBoarder"/>
</wsdl:message>
<!-- response GetEndorsingBoarderResponse is of type
GetEndorsingBoarderResponse -->
<wsdl:message name="GetEndorsingBoarderResponse">
<wsdl:part name="body" element="esxsd:GetEndorsingBoarderResponse"/>
</wsdl:message>
<!-- wsdl:portType describes messages in an operation -->
<wsdl:portType name="GetEndorsingBoarderPortType">
<!-- the value of wsdl:operation eludes me -->
<wsdl:operation name="GetEndorsingBoarder">
<wsdl:input message="es:GetEndorsingBoarderRequest"/>
<wsdl:output message="es:GetEndorsingBoarderResponse"/>
<wsdl:fault message="es:GetEndorsingBoarderFault"/>
</wsdl:operation>
</wsdl:portType>
<!-- wsdl:binding states a serialization protocol for this service -->
<wsdl:binding name="EndorsementSearchSoapBinding"
type="es:GetEndorsingBoarderPortType">
<!-- leverage off soap:binding document style ...(no wsdl:foo pointing at
the soap binding) -->
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<!-- semi-opaque container of network transport details classed by
soap:binding above ... -->
<wsdl:operation name="GetEndorsingBoarder">
<!-- again bind to SOAP? ... -->
<soap:operation soapAction="http://www.snowboard-info.com/
EndorsementSearch"/>
<!-- further specify that the messages in the wsdl:operation
"GetEndorsingBoarder" use SOAP? ... -->
<wsdl:input>
<soap:body use="literal"
namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"
namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>
</wsdl:output>
<wsdl:fault>
<soap:body use="literal"
namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<!-- wsdl:service names a new service "EndorsementSearchService" -->
<wsdl:service name="EndorsementSearchService">
<wsdl:documentation>snowboarding-info.com Endorsement Service</
wsdl:documentation>
<!-- connect it to the binding "EndorsementSearchSoapBinding" above -->
<wsdl:port name="GetEndorsingBoarderPort"
binding="es:EndorsementSearchSoapBinding">
<!-- give the binding an network address -->
<soap:address location="http://www.snowboard-info.com/EndorsementSearch"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
WSDL 聲明了消息類型、默認數據類型和內容以及交換的數據結構。
訪問服務器上 SOAP 結構需要使用的一切信息都可以在這個 WSDL 中找到。大多數語言和環境都提供一種閱讀和解析 WSDL 的機制,以確定可用的函數和數據交換。
WSDL 不僅定義了用於交換信息的 SOAP 接口,通過適當的 WSDL 生成程序,還可用於創建發送請求、生成並格式化響應所需要的代碼。
WSDL 和 SOAP 組成了一個強大的遠程過程調用系統。