WSDL文件详解(转贴)中

详解

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>

时间: 2024-07-30 10:55:36

WSDL文件详解(转贴)中的相关文章

WSDL文件详解(转贴)上

详解 作者:Carlos C. TapangInfotects 2001 年 7 月 摘要:只要使用 WSDL,即可以真正不受語言與平台限制的方式,自動為網路服務產生 Proxy.(列印共 28 頁) 內容使用 WSDL 的原因WSDL 文件結構WSDL 範例檔案命名空間SOAP 訊息WSDL 類型與訊息區段中的 XML 結構描述<portType> 與 <operation> 元素<binding> 與 <operation> 元素文件樣式繫結<se

WSDL文件详解(转贴)下

详解 在本文寫作時,並無任何 SOAP 實作,能進行作業名稱的多載動作.這對使用 Java 的客戶端是很重要的,因為使用 Java 的伺服端所使用的介面,會利用 Java 的多載功能.但這對使用 COM 的客戶端倒無所謂,因為 COM 並不支援多載. <binding> 與 <operation> 元素Binding 區段是,指定通訊協定.序列化.與編碼的地方.若 Types.Messages.與 PortType 等區段負責抽象的資料內容,那麼 Binding 區段便負責處理資料

Python中使用不同编码读写txt文件详解

  这篇文章主要介绍了Python中使用不同编码读写txt文件详解,本文给出不同编码下的读写文件代码方法,需要的朋友可以参考下 代码如下: import os import codecs filenames=os.listdir(os.getcwd()) out=file("name.txt","w") for filename in filenames: out.write(filename.decode("gb2312").encode(&q

Windows CE系统开发,BSP包中的几个常见文件详解

Windows CE系统开发,BSP包中的几个常见文件详解 BSP介绍(Board Support Package)是介于底层硬件和上层软件之间的底层软件开发包,它主要功能为屏蔽硬件,提供操作系统及硬件驱动,具体功能包括: (1)      单板硬件初始化,主要是CPU的初始化,为整个软件系统提供底层硬件支持: (2)      为操作系统提供设备驱动程序和系统中断服务程序: (3)      定制操作系统的功能,为软件系统提供一个实时多任务的运行环境: (4)      初始化操作系统,为操作

详解安卓系统中的Android.mk文件_C 语言

概述    Android.mk文件用来向编译系统描述如何编译你的源代码.更确切地说,该文件其实就是一个小型的Makefile.由于该文件会被NDK的编译工具解析多次,因此应该尽量减少源码中声明变量,因为这些变量可能会被多次定义从而影响到后面的解析.这个文件的语法允许把源代码组织成模块,每个模块属于下列类型之一:     APK程序:一般的Android程序,编译打包生成apk文件.     JAVA库:java类库,编译打包生成jar包文件.     C\C++应用程序:可执行的C/C++应用

Android 中Manifest.xml文件详解

Android 中Manifest.xml文件详解 每一个Android项目都包含一个清单(Manifest)文件--AndroidManifest.xml,它存储在项目层次中的最底层.清单可以定义应用程序及其组件的结构和元数据. 它包含了组成应用程序的每一个组件(活动.服务.内容提供器和广播接收器)的节点,并使用Intent过滤器和权限来确定这些组件之间以及这些组件和其他应用程序是如何交互的. 它还提供了各种属性来详细地说明应用程序的元数据(如它的图标或者主题)以及额外的可用来进行安全设置和单

MySQL日志文件详解

  这篇文章主要介绍了MySQL日志文件详解,本文分别讲解了错误日志.二进制日志.通用查询日志.慢查询日志.Innodb的在线redo日志.更新日志等日志类型和作用介绍,需要的朋友可以参考下 概述 日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志可以帮助我们定位mysqld内部发生的事件,数据库性能故障,记录数据的变更历史,用户恢复数据库等等.本文主要描述MySQL的各种日志文件. MySQL日志文件

详解Linux系统中md5sum命令的用法

  这篇文章主要介绍了详解Linux系统中md5sum命令的用法,用来处理MD5验证的相关操作,需要的朋友可以参考下 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意长度的信息逐位进行计算,产生一个二进制长度为128位(十六进制长度就是32位)的"指纹"(或称"报文摘要"),不同的文件产生相 同的报文摘要的可能性是非常非常之小的. 在linux或Unix上,

详解iOS App中UITableView的创建与内容刷新_IOS

UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件. 创建首先创建一个新的项目,并添加一个MainViewController的Class文件 打开MainViewController.h文件 @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, retain) NSArray *