WSDL 是基於 XML 的用於描述 Web Services 以及如何訪問 Web Services 的語言。
什麼是 WSDL?
- WSDL 指網絡服務描述語言
- WSDL 使用 XML 編寫
- WSDL 是一種 XML 文檔
- WSDL 用於描述網絡服務
- WSDL 也可用於定位網絡服務
- WSDL 還不是 W3C 標準
WSDL 可描述網絡服務(Web Services)
WSDL 指網絡服務描述語言 (Web Services Description Language)。
WSDL 是一種使用 XML 編寫的文檔。這種文檔可描述某個 Web service。它可規定服務的位置,以及此服務提供的操作(或方法)。
在 W3C 的 WSDL 發展史
在 2001 年 3 月,WSDL 1.1 被 IBM、微軟作爲一個 W3C 紀錄(W3C note)提交到有關 XML 協議的 W3C XML 活動,用於描述網絡服務。
(W3C 紀錄僅供討論。一項 W3C 紀錄的發佈並不代表它已被 W3C 或 W3C 團隊亦或任何 W3C 成員認可。)
在 2002 年 7 月,W3C 發佈了第一個 WSDL 1.2 工作草案。
請在我們的 W3C 教程 閱讀更多有關規範的狀態及時間線。
WSDL 文檔僅僅是一個簡單的 XML 文檔。
它包含一系列描述某個 web service 的定義。
WSDL 文檔結構
WSDL 文檔是利用這些主要的元素來描述某個 web service 的:
元素 | 定義 |
---|---|
<portType> | web service 執行的操作 |
<message> | web service 使用的消息 |
<types> | web service 使用的數據類型 |
<binding> | web service 使用的通信協議 |
一個 WSDL 文檔的主要結構是類似這樣的:
<definitions> <types>definition of types........
</types> <message>definition of a message....
</message> <portType>definition of a port.......
</portType> <binding>definition of a binding....
</binding> </definitions>
WSDL 文檔可包含其它的元素,比如 extension 元素,以及一個 service 元素,此元素可把若干個 web services 的定義組合在一個單一的 WSDL 文檔中。
如需完整的語法概述,請訪問 WSDL 語法 這一節。
WSDL 端口
<portType> 元素是最重要的 WSDL 元素。
它可描述一個 web service、可被執行的操作,以及相關的消息。
可以把 <portType> 元素比作傳統編程語言中的一個函數庫(或一個模塊、或一個類)。
WSDL 消息
<message> 元素定義一個操作的數據元素。
每個消息均由一個或多個部件組成。可以把這些部件比作傳統編程語言中一個函數調用的參數。
WSDL types
<types> 元素定義 web service 使用的數據類型。
爲了最大程度的平臺中立性,WSDL 使用 XML Schema 語法來定義數據類型。
WSDL Bindings
<binding> 元素爲每個端口定義消息格式和協議細節。
WSDL 實例
這是某個 WSDL 文檔的簡化的片段:
<message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType>
在這個例子中,<portType> 元素把 "glossaryTerms" 定義爲某個端口的名稱,把 "getTerm" 定義爲某個操作的名稱。
操作 "getTerm" 擁有一個名爲 "getTermRequest" 的輸入消息,以及一個名爲 "getTermResponse" 的輸出消息。
<message> 元素可定義每個消息的部件,以及相關聯的數據類型。
對比傳統的編程,glossaryTerms 是一個函數庫,而 "getTerm" 是帶有輸入參數 "getTermRequest" 和返回參數 getTermResponse 的一個函數。
<wsdl:definitions name="nmtoken"? targetNamespace="uri"> <import namespace="uri" location="uri"/> * <wsdl:documentation .... /> ? <wsdl:types> ? <wsdl:documentation .... /> ? <xsd:schema .... /> * </wsdl:types> <wsdl:message name="ncname"> * <wsdl:documentation .... /> ? <part name="ncname" element="qname"? type="qname"?/> * </wsdl:message> <wsdl:portType name="ncname"> * <wsdl:documentation .... /> ? <wsdl:operation name="ncname"> * <wsdl:documentation .... /> ? <wsdl:input message="qname"> ? <wsdl:documentation .... /> ? </wsdl:input> <wsdl:output message="qname"> ? <wsdl:documentation .... /> ? </wsdl:output> <wsdl:fault name="ncname" message="qname"> * <wsdl:documentation .... /> ? </wsdl:fault> </wsdl:operation> </wsdl:portType> <wsdl:serviceType name="ncname"> * <wsdl:portType name="qname"/> + </wsdl:serviceType> <wsdl:binding name="ncname" type="qname"> * <wsdl:documentation .... /> ? <-- binding details --> * <wsdl:operation name="ncname"> * <wsdl:documentation .... /> ? <-- binding details --> * <wsdl:input> ? <wsdl:documentation .... /> ? <-- binding details --> </wsdl:input> <wsdl:output> ? <wsdl:documentation .... /> ? <-- binding details --> * </wsdl:output> <wsdl:fault name="ncname"> * <wsdl:documentation .... /> ? <-- binding details --> * </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="ncname" serviceType="qname"> * <wsdl:documentation .... /> ? <wsdl:port name="ncname" binding="qname"> * <wsdl:documentation .... /> ? <-- address details --> </wsdl:port> </wsdl:service> </wsdl:definitions>