<element attribute="attribute-value">contents</element> |
|
WSDL文件示例
让我们来研究一下WSDL文件,看看它的结构,以及如何工作。请注意这是一个非常简单的WSDL文档实例。我们的意图只是说明它最显著的特征。以下的内容中包括更加详细的讨论。
<?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); |
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 |
SOAP消息
对于使用WSDL的客户机和服务机来说,研究WSDL文件的一种方法就是决定什么来接受所发送的信息。尽管SOAP使用底层协议,如IP和HTTP等,但应用程序决定了服务器与客户机之间交互的高级协议。也就是说,进行一项操作,比如"echoint"把输入的整数送回,参数的数目、每个参数的类型、以及参数如何传送等因素决定了应用程序特定的协议。有很多方法可以确定此类协议,但我相信最好的方法就是使用WSDL。如果我们用这种视角来看待它,WSDL不只是一种接口协议,而且是一种协议特定的语言。它就是我们超越"固定"协议(IP、HTTP等)所需要的应用程序特定协议。
WSDL可以确定SOAP消息是否遵从RPC或文档风格。RPC风格的消息(就是示例中所用的)看起来像是函数调用。而文档风格的消息则更普通,嵌套层次更小。下面的XML消息就是示例WSDL文件解析后的发送/接受效果,解析使用的是MS SOAP Toolkit 2.0(MSTK2)中的SoapClient对象。
从客户端调用"foo(5131953)"函数:
<?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> |
<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> |
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 |
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> |
新的complex类型从soapenc:array限制派生。然后又声明了complex类型的一个属性。引用"soapenc:arrayType"实际上是这样完成的:
<xsd:attribute name="arrayType" type="xsd:string"/> |
wsdl:arrayType属性值决定了数组每个成员的类型。数组的成员也可以是Complex类型。:
<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> |
WSDL要求数组的类型由"ArrayOf"和每个数组元素的类型串联而成。很显然,顾名思义,"ArrayOfPERSON"是PERSON结构的数组。下面我将使用ArrayOfPERSON来声明一个<message>,并加入不止一个PERSON:
<?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函数:
void foo(int arg); void foo(string arg); |
<?xml version="1.0" encoding="UTF-8" ?> <definitions name="fooDescription" 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="foo1"> <part name="arg" type="xsd:int"/> </message> <message name="foo2"> <part name="arg" type="xsd:string"/> </message> <portType name="fooSamplePortType"> <operation name="foo" parameterOrder="arg " > <input name="foo1" message="wsdlns:foo1"/> </operation> <operation name="foo" parameterOrder="arg " > <input name="foo2" message="wsdlns:foo2"/> </operation> </portType> <binding name="fooSampleBinding" type="wsdlns:fooSamplePortType"> <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/foo1"/> <input name="foo1"> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> </operation> <operation name="foo"> <soap:operation soapAction="http://tempuri.org/action/foo2"/> <input name="foo2"> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> </operation> </binding> <service name="FOOService"> <port name="fooSamplePort" binding="fooSampleBinding"> <soap:address location="http://carlos:8080/fooService/foo.asp"/> </port> </service> </definitions> |
<binding>和<operation>元素
Binding栏是完整描述协议、序列化和编码的地方,Types, Messages和PortType栏处理抽象的数据内容,而Binding栏是处理数据传输的物理实现。Binding栏把前三部分的抽象定义具体化。
把相关的数据制定和消息声明分开,这意味着同一类型服务的提供者可以把一系列的操作标准化。每个提供者可以提供定制的binding来互相区分。WSDL也有一个重要的结构,使抽象定义可以放在分离的文件中,而不是和Bindings和Services在一起,这样可在不同的服务提供者之间提供标准化的抽象定义,这很有帮助。例如,银行可以用WSDL文档来标准化一些银行的操作。每个银行仍然可以自由的订制下层的协议、串行优化,及编码。
下面是重载的WSDL示例 的Binding栏,重复在此以便讨论:
<binding name="fooSampleBinding" type="wsdlns:fooSamplePortType"> <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/foo1"/> <input name="foo1"> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" /> </input> </operation> <operation name="foo"> <soap:operation soapAction="http://tempuri.org/action/foo2"/> <input name="foo2"> <soap:body use="encoded" namespace="http://tempuri.org/message/" encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" /> </input> </operation> </binding> |
"http://schemas.xmlsoap.org/soap/encoding" |
文档风格实现
在前几栏中,<soap:binding>元素有一个类型属性,设为"rpc"。此属性设为"document"时会改变传输时消息的串行化。不同于函数签名,现在的消息是文档传输的。在这类binding中,<message>元素定义文档格式,而不是函数签名。作为例子,考虑以下WSDL片段:
<definitions xmlns:stns="(SchemaTNS)" xmlns:wtns="(WsdlTNS)" targetNamespace="(WsdlTNS)"> <schema targetNamespace="(SchemaTNS)" elementFormDefault="qualified"> <element name="SimpleElement" type="xsd:int"/> <element name="CompositElement" type="stns:CompositeType"/> <complexType name="CompositeType"> <all> <element name='a' type="xsd:int"/> <element name='b' type="xsd:string"/> </all> </complexType> </schema> <message...> <part name='p1' type="stns:CompositeType"/> <part name='p2' type="xsd:int"/> <part name='p3' element="stns:SimpleElement"/> <part name='p4' element="stns:CompositeElement"/> </message> … </definitions> |
<service name="FOOService"> <port name="fooSamplePort" binding="fooSampleBinding"> <soap:address location="http://carlos:8080/fooService/foo.asp"/> </port> </service> |
元素 | 属性 | 内容(子元素) |
<definitions> | name targetNamespace xmlns (other namespaces) |
<types> <message> <portType> <binding> <service> |
<types> | (none) | <xsd:schema> |
<message> | Name | <part> |
<portType> | Name | <operation> |
<binding> | name type |
<operation> |
<service> | name | <port> |
<part> | name type |
(empty) |
<operation> | name parameterOrder |
<input> <output> <fault> |
<input> | name message |
(empty) |
<output> | name message |
(empty) |
<fault> | name message |
(empty) |
<port> | name binding |
<soap:address> |