html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

WSDL文件详解(转贴)中_.NET教程_编程技术

[ ] 已经帮助:人解决问题
详解
WSDL 型息段中的 XML 描述
WSDL 的料型,是根目前 W3C Recommendation 的「XML Schema: Datatypes」(XSD)。此文件共有三不同的版本 (1999、2000/10、 2001),若欲指定特定 WSDL 案所使用的版本,在 <definitions> 元素中,其宣告命名空的性。方法如下:

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

本文以 2001 版考量。WSDL 的者,也大力建使用 2001 版。

在本後章中,用的字首或命名空速法如下:

字首 的命名空 明
soapenc http://schemas.xmlsoap.org/soap/encoding SOAP 1.1
wsdl http://schemas.xmlsoap.org/wsdl/soap WSDL 1.1
xsd http://www.w3.org/2001/XMLSchema XML Schema

XSD 基本型
下表直接取自 MSTK2 文件,列了 MSTK2 支援的所有 XSD 基本型。表明,位於客端伺服端的 WSDL 者,如何在 VB、C++、 IDL 中, XSD 型至不同等的型。

XSD (Soap) 型 不同的型 VB C++ IDL 解
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 不行或
nonNegativeInteger VT_DECIMAL Variant DECIMAL DECIMAL 。
nonPositiveInteger VT_DECIMAL Variant DECIMAL 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 Day 定成 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 。

XSD 定建的料型:基本型衍生型。若需一步,可前往 http://www.w3.org/TR/2001/PR-xmlschema-2-20010330,建型的架。


XML Schema 可定型,也就是 C 中的 struct。例如,下列 C struct 的相定方式:

typedef struct {
string firstName;
string lastName;
long ageInYears;
float weightInLbs;
float heightInInches;
} PERSON;

若使用 XML Schema 可以撰成:

<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>

不,<complexType> 所能表示的,不止於 struct 的而已。除了 <sequence> 之外,它可以有其它的子元素。若不用 <sequence>,也可以使用 <all>:

<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>

其意是,成 <element> 可以任何序入,且每一都具有性。便 C struct 的使用方式不同了。

注意例中,string (字串)、int (整)、float (浮) 等建料型的使用方式。C 的字串在 XML 中也是字串,且浮是浮。但 C 的 long (整),在 XML 是 int (考上表)。

在 WSDL 案中,Types 段是宣告上述型的位置。例如,PERSON 型可以下列方式宣告,其用於 Messages 段中:

<?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>

在上述的例中,第一息的名是「addPerson」,它有型「PERSON」的 <part>。在 Types 段中,型 PERSON 被宣告型。

起始 MSTK2 SoapClient ,若在上述片段使用完整的 WSDL 案,它便可成功地剖析案。不,它是不能函呼叫,送至 <addPerson>。是因 SoapClient 本身不知道如何理型;它需要自的型器 (mapper) 才能理型。在 MSTK2 文件中有一例用程式,它含有自的型器。

另外有一方法,可 <part> 元素,至型宣告。方法使用的是元素,而非型性。下例在 Types 段中,先宣告元素 (「Person」「Gender」);然後在「addPerson」的 <message> 中,再使用元素性照它。

<?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>

在 Types 段的 Gender <element> 中,嵌著一匿名的列型,其值可以是「Male」或「Female」。然後在「addPerson」的 <message> 中,再使用元素性 (不用型性) 照元素。

若欲特定型至 <part>,使用「元素」「型」性有何不同?若使用「型」性,part 可描述成能取型 (就像一);但若使用「元素」性,便不能做。考下列例的明。

<?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>

此例也明了副名的衍生用法。「femalePerson」「malePerson」者,都是衍生自「PERSON」。它都各有一外的元素:「femalePerson」的「favoriteLipstick」以及「malePerson」的「favoriteShavingLotion」。使用 <choice> 的 construct,衍生型又可合成一型「maleOrFemalePerson」。最後,在「addPerson」的 <message> 中,此合型又可供「person」的 <part> 照。而此 <part> 或,可以是「femalePerson」或「malePerson」。


XSD 可提供 <list> construct,以宣告空白所分隔的目列。但是,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>

只要使用出限制的方式,即可 soapenc:Array 宣告新的型。接著便可宣告此型的性:arrayType。照「soapenc:arrayType」上即是 arrayType 性的宣告,其方式如下:

<xsd:attribute name="arrayType" type="xsd:string"/>

然後,wsdl:arrayType 性值,可定每列成的型。列目也可以是型:

<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」列目型的的串 (concatenation)。也因此,名即可得知,「ArrayOfPERSON」是 PERSON struct 的列。在下例中,只要使用 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 中的作元素,可定呼叫所有 PortType 方法的法。每作元素都宣告,方法的名、 (使用 <message> 元素)、型 (每 <message> 中所宣告的 <part> 元素)。

在 WSDL 文件中,可有多 <portType> 元素。每 <portType> 元素,群化多相作的方式, COM 介面群化方法的方式非常似。

在一 <operation> 元素中,最多可有一 <input> 元素、一 <output> 元素、一 <fault> 元素。三元素都各有一名息性。

在 <input>、<output>、 <fault> 元素中使用名性的目的何?原是了,具相同名 (多) 的作。例如,下列 C 函,即具有相同的名,但不同的。

void foo(int arg);
void foo(string arg);

使用 WSDL ,多的表方式如下:

<?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>
cSCHTML5中文学习网 - HTML5先行者学习网
cSCHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助