为什么使用WSDL?
像Internet协议之类的标准有没有为权威所利用,或者人们这样看待它是因为顺之所获的好处远远超出了代价?曾经有许多试图建立的标准都流产了。有时候,那些还没有普遍使用的标准甚至由法令或政府规定强行推出:Ada语言就是一例。
我相信正是跟随标准所带来的好处使它广泛接受。例如,对于铁路服务来说,真正重要的是,不同公司所铺设的铁路结合到一起,或者是来自好几个公司的产品协调的工作在一起。几家大的企业合力建立了SOAP标准。Web Service描述语言(WSDL)向这种Web Service的提供商和用户推出了方便的协调工作的方法,使我们能更容易的获得SOAP的种种好处。几家公司的铁道并在一起不算什么难事,他们所需遵循的只是两轨间的标准距离。对Web Service来说,这要复杂得多。我们必须先制定出指定接口的标准格式。
曾经有人说SOAP并不真需要什么接口描述语言。如果SOAP是交流纯内容的标准,那就需要一种语言来描述内容。SOAP消息确实带有某些类型信息,因此SOAP允许动态的决定类型。但不知道一个函数的函数名、参数的个数和各自类型,怎么可能去调用这个函数呢?没有WSDL,我可以从必备文档中确定调用语法,或者检查消息。随便何种方法,都必须有人参与,这个过程可能会有错。而使用了WSDL,我就可以通过这种跨平台和跨语言的方法使Web Service代理的产生自动化。就像COM和CORBA的IDL文件,WSDL文件由客户和服务器约定。
注意由于WSDL设计成可以绑定除SOAP以外的其他协议,这里我们主要关注WSDL在HTTP上和SOAP的关系。同样,由于SOAP目前主要用来调用远程的过程和函数,WSDL支持SOAP传输的文档规范。WSDL 1.1已经作为记录递交给W3C(见http://www.w3.org/TR/wsdl.html)
WSDL文档结构
若要理解XML文档,将之看作块状图表非常有用。下图以XML的文档形式说明了WSDL的结构,它揭示了WSDL文档五个栏之间的关系。
WSDL文档可以分为两部分。顶部分由抽象定义组成,而底部分则由具体描述组成。抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随机器或语言而变的元素。这就定义了一系列服务,截然不同的网站都可以实现。随网站而异的东西如序列化便归入底部分,因为它包含具体的定义。
l 抽象定义
Types
独立与机器和语言的类型定义
Messages
包括函数参数(输入与输出分开)或文档描述
PortTypes
引用消息部分中消息定义来描述函数签名(操作名、输入参数、输出参数)
2 具体定义
Bindings
PortTypes部分的每一操作在此绑定实现
Services
确定每一绑定的端口地址
下面的图中,箭头连接符代表文档不同栏之间的关系。点和箭头代表了引用或使用关系。双箭头代表"修改"关系。3-D的箭头代表了包含关系。这样,各Messages栏使用Types栏的定义,PortTypes栏使用Messages栏的定义;Bindings栏引用了PortTypes栏,Services栏引用Bindings栏,PortTypes和Bindings栏包含了operation元素,而Services栏包含了port元素。PortTypes栏里的operation元素由Bindings栏里的operation元素进一步修改或描述。
在此背景中,我将使用标准的XML术语来描述WSDL文档。Element是指XML的元素,而"attribute"指元素的属性。于是:
<element attribute="attribute-value">contents</element> |
图一:抽象定义和具体定义 |
<?xml version="1.0" encoding="UTF-8" ?> <definitions name="FooSample" targetNamespace="http://tempuri.org/wsdl/" xmlns:wsdlns="http://tempuri.org/wsdl/" xmlns:typens="http://tempuri.org/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:stk="http://schemas.microsoft.com/soap-toolkit/wsdl-extension" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <schema targetNamespace="http://tempuri.org/xsd" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" elementFormDefault="qualified" > </schema> </types> <message name="Simple.foo"> <part name="arg" type="xsd:int"/> </message> <message name="Simple.fooResponse"> <part name="result" type="xsd:int"/> </message> <portType name="SimplePortType"> <operation name="foo" parameterOrder="arg" > <input message="wsdlns:Simple.foo"/> <output message="wsdlns:Simple.fooResponse"/> </operation> </portType> <binding name="SimpleBinding" type="wsdlns:SimplePortType"> <stk:binding preferredEncoding="UTF-8" /> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="foo"> <soap:operation soapAction="http://tempuri.org/action/Simple.foo"/> <input> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> </binding> <service name="FOOSAMPLEService"> <port name="SimplePort" binding="wsdlns:SimpleBinding"> <soap:address location="http://carlos:8080/FooSample/FooSample.asp"/> </port> </service> </definitions> |
int foo(int arg); |
这个例子足见XML和C相比要冗长的多。(包括<message>元素,XML在示例中共使用了12行代码来表达相同的单行函数声明。)
Bindings栏可以有零个、一个或者多个<binding>元素。它的意图是制定每个<operation>通过网络调用和回应。Services栏同样可以有零个、一个、多个<service>元素。它还包含了<port>元素,每个<port>元素引用一个Bindings栏里的<binding>元素。Bindings和Services栏都包含WSDL文档。
Namespace
<definitions>和子节点<schema>都是namespace属性:
<definitions name="FooSample" targetNamespace="http://tempuri.org/wsdl/" xmlns:wsdlns="http://tempuri.org/wsdl/" xmlns:typens="http://tempuri.org/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:stk="http://schemas.microsoft.com/soap-toolkit/wsdl-extension" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <schema targetNamespace="http://tempuri.org/xsd" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" elementFormDefault="qualified" > </schema> </types> |
xmlns="http://www.w3.org/2001/XMLSchema" |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <m:foo xmlns:m="http://tempuri.org/message/"> <arg>5131953</arg> </m:foo> </SOAP-ENV:Body> </SOAP-ENV:Envelope> 从服务器接受的信息: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <SOAPSDK1:fooResponse xmlns:SOAPSDK1="http://tempuri.org/message/"> <result>5131953</result> </SOAPSDK1:fooResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> |
<stk:binding preferredEncoding="UTF-8" /> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="foo"> <soap:operation soapAction="http://tempuri.org/action/Simple.foo"/> <input> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> </binding>
|
WSDL的Types栏和Messages栏中的XML Schema
WSDL数据类型是基于"XML Schema: Datatypes"(XSD)的,现在已经被W3C推荐。这一文档共有三个版本(1999,2000/10,2001),因此必须在namespace属性的<definitions>元素中指明所使用的是哪一个版本。
xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
前缀 | 代表的Namespace | 描述 |
Soapenc | http://schemas.xmlsoap.org/soap/encoding | SOAP 1.1 encoding |
Wsdl | http://schemas.xmlsoap.org/wsdl/soap | WSDL 1.1 |
Xsd | http://www.w3.org/2001/XMLSchema | XML Schema |
XSD (Soap)类型 | 变量类型 | VB | C++ | IDL | Comments |
anyURI | VT_BSTR | String | BSTR | BSTR | |
base64Binary | VT_ARRAY | VT_UI1 | Byte() | SAFEARRAY | SAFEARRAY(unsigned char) | |
Boolean | VT_BOOL | Boolean | VARIANT_BOOL | VARIANT_BOOL | |
Byte | VT_I2 | Integer | short | short | 转换时验证范围有效性 |
Date | VT_DATE | Date | DATE | DATE | 时间设为 oo:oo:oo |
DateTime | VT_DATE | Date | DATE | DATE | |
Double | VT_R8 | Double | double | double | |
Duration | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
ENTITIES | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
ENTITY | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
Float | VT_R4 | Single | float | float | |
GDay | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
GMonth | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
GMonthDay | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
GYear | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
GYearMonth | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
ID | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
IDREF | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
IDREFS | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
Int | VT_I4 | Long | long | long | |
Integer | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
Language | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
Long | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
Name | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
NCName | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
negativeInteger | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
NMTOKEN | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
NMTOKENS | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
nonNegativeIntege | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
nonPositiveInteger | VT_DECIMAL | Variant | DECIMA | DECIMAL | 转换时范围生效 |
normalizedString | VT_BSTR | String | BSTR | BSTR | |
NOTATION | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
Number | VT_DECIMAL | Variant | DECIMAL | DECIMAL | |
positiveInteger | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
Qname | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
Short | VT_I2 | Integer | short | short | |
String | VT_BSTR | String | BSTR | BSTR | |
Time | VT_DATE | Date | DATE | DATE | 日设为1899年12月30日 |
Token | VT_BSTR | String | BSTR | BSTR | 不转换和生效 |
unsignedByte | VT_UI1 | Byte | unsigned char | unsigned char | |
UnsignedInt | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
unsignedLong | VT_DECIMAL | Variant | DECIMAL | DECIMAL | 转换时范围生效 |
unsignedShort | VT_UI4 | Long | Long | Long | 转换时范围生效 |
http://www.w3.org/TR/2001/PR-xmlschema-2-20010330 |
complex类型
XML schema允许complex类型的定义,就像C里是struct。例如,为了定义类似如下的C的struct类型:
typedef struct { string firstName; string lastName; long ageInYears; float weightInLbs; float heightInInches; } PERSON; |
<xsd:complexType name="PERSON"> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="ageInYears" type="xsd:int"/> <xsd:element name="weightInLbs" type="xsd:float"/> <xsd:element name="heightInInches" type="xsd:float"/> </xsd:sequence> </xsd:complexType> |
<xsd:complexType name="PERSON"> <xsd:all> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="ageInYears" type="xsd:int"/> <xsd:element name="weightInLbs" type="xsd:float"/> <xsd:element name="heightInInches" type="xsd:float"/> </xsd:all> </xsd:complexType> |
<?xml version="1.0" encoding="UTF-8" ?> <definitions … > <types> <schema targetNamespace="someNamespace" xmlns:typens="someNamespace" > <xsd:complexType name="PERSON"> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="ageInYears" type="xsd:int"/> <xsd:element name="weightInLbs" type="xsd:float"/> <xsd:element name="heightInInches" type="xsd:float"/> </xsd:sequence> </xsd:complexType> </schema> </types> <message name="addPerson"> <part name="person" type="typens:PERSON"/> </message> <message name="addPersonResponse"> <part name="result" type="xsd:int"/> </message> </definitions> |
<?xml version="1.0" encoding="UTF-8" ?> <definitions … > <types> <schema targetNamespace="someNamespace" xmlns:typens="someNamespace" > <element name="Person"> <xsd:complexType> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="ageInYears" type="xsd:int"/> <xsd:element name="weightInLbs" type="xsd:float"/> <xsd:element name="heightInInches" type="xsd:float"/> </xsd:sequence> </xsd:complexType> </element> <element name="Gender"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Male" /> <xsd:enumeration value="Female" /> </xsd:restriction> </xsd:simpleType> </element> </schema> </types> <message name="addPerson"> <part name="who" element="typens:Person"/> <part name="sex" element="typens:Gender"/> </message> <message name="addPersonResponse"> <part name="result" type="xsd:int"/> </message> </definitions> |
<?xml version="1.0" encoding="UTF-8" ?> <definitions … > <types> <schema targetNamespace="someNamespace" xmlns:typens="someNamespace"> <xsd:complexType name="PERSON"> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="ageInYears" type="xsd:int"/> <xsd:element name="weightInLbs" type="xsd:float"/> <xsd:element name="heightInInches" type="xsd:float"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="femalePerson"> <xsd:complexContent> <xsd:extension base="typens:PERSON" > <xsd:element name="favoriteLipstick" type="xsd:string" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="malePerson"> <xsd:complexContent> <xsd:extension base="typens:PERSON" > <xsd:element name="favoriteShavingLotion" type="xsd:string" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="maleOrFemalePerson"> <xsd:choice> <xsd:element name="fArg" type="typens:femalePerson" > <xsd:element name="mArg" type="typens:malePerson" /> </xsd:choice> </xsd:complexType> </schema> </types> <message name="addPerson"> <part name="person" type="typens:maleOrFemalePerson"/> </message> <message name="addPersonResponse"> <part name="result" type="xsd:int"/> </message> </definitions> |
上例也告诉我们extension的派生。"femailPerson"和"malePerson"都是从"PERSON"派生出来的。它们各有一些额外的元素:"femalePerson"有"favoriteLipstick"元素,"malePerson"有"favoriteShavingLotion"元素。两派生类型都归入一个complex类型"maleOrFemalePerson",使用的是<choice>构造。最后,在"adperson"<message>中,新类型有"person"<part>引用。这样,参数或<part>就可以是"femalePerson"或"malePerson"了。
数组
XSD提供<list>结构来声明一个数组,元素之间有空格界定。不过SOAP不是使用XSD来编码数组的,它定义了自己的数组类型--"SOAP-ENC: Array"。下列的例子揭示了从这一类型派生出一位整数数组的方法:
<xsd:complexType name="ArrayOfInt"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:int[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> |
<xsd:attribute name="arrayType" type="xsd:string"/> |
<xsd:complexType name="ArrayOfPERSON"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:PERSON[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> |
<?xml version="1.0" encoding="UTF-8" ?> <definitions … > <types> <schema targetNamespace="someNamespace" xmlns:typens="someNamespace" > <xsd:complexType name="PERSON"> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="ageInYears" type="xsd:int"/> <xsd:element name="weightInLbs" type="xsd:float"/> <xsd:element name="heightInInches" type="xsd:float"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="ArrayOfPERSON"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:PERSON[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </schema> </types> <message name="addPersons"> <part name="person" type="typens:ArrayOfPERSON"/> </message> <message name="addPersonResponse"> <part name="result" type="xsd:int"/> </message> </definitions> |
<portType>和<operation>元素 PortType定义了一些抽象的操作。PortType中的operation元素定义了调用PortType中所有方法的语法,每一个operation元素声明了方法的名称、参数(使用<message>元素)和各自的类型(<part>元素要在所有<message>中声明)。 在一篇WSDL文档中可以有几个<PortType>元素,每一个都和一些相关操作放在一起,就和COM和一组操作的接口相似。 在<operation>元素中,可能会有至多一个<input>元素,一个<output>元素,以及一个<fault>元素。三个元素各有一个名字和一个消息属性。 <input>, <output>, <fault>元素属性的名字有何含义呢?它们可以用来区别两个同名操作(重载)。例如,看下面两个C函数:
这种重载在WSDL中可以这样表示:
|