完整的 WSDL 语法
WSDL(Web Services Description Language)是一种用于描述Web服务的语言,可以提供对Web服务的详细描述,包括服务的功能、参数、格式和支持的协议等。本文将介绍WSDL最基本的语法结构。
文档声明
每个WSDL文档都需要包含XML文档声明,例如:
<?xml version="1.0" encoding="UTF-8"?>
命名空间
WSDL文档的根元素必须指定命名空间,例如:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" ...>
元素结构
WSDL文档由多个元素组成,其中最常见的元素包括:
definitions
该元素是WSDL文档的根元素,必须包含在所有其他元素之外。其作用是定义命名空间、命名空间前缀和所有可用操作。
types
该元素用于定义本地数据类型、参数类型和返回值类型。它与XML Schema相似。
message
该元素用于定义在Web服务中使用的消息,包括输入消息和输出消息。
portType
该元素用于定义Web服务所支持的一组操作,在每个操作中,客户端将发送一条消息,服务器将发送一条响应消息。每个操作都包含输入和输出消息。
binding
该元素用于将一个portType与一组协议绑定,例如SOAP、HTTP等。这样可以定义如何将消息从客户端发送到服务器以及如何将响应消息从服务器发送回客户端。
service
该元素用于定义一个Web服务以及其所支持的端口、绑定和访问地址。
示例
以下是一个简单的WSDL文档示例:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example.com/webservice/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/webservice/">
<message name="helloRequest">
<part name="name" type="xsd:string"/>
</message>
<message name="helloResponse">
<part name="message" type="xsd:string"/>
</message>
<portType name="helloPortType">
<operation name="sayHello">
<input message="tns:helloRequest"/>
<output message="tns:helloResponse"/>
</operation>
</portType>
<binding name="helloBinding" type="tns:helloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="helloService">
<port name="helloPort" binding="tns:helloBinding">
<soap:address location="http://example.com/hello/"/>
</port>
</service>
</definitions>
该文档定义了一个名为"helloService"的WebService服务,其唯一端口名为"helloPort",功能是向服务端发送一个名为"sayHello"的请求,以便将消息传递到服务端。服务端接收消息后会将一个包含欢迎信息的响应返回给客户端。