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>