天天看點

 WSDL檔案示例

 <b>WSDL檔案示例</b>

  讓我們來研究一下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"/>

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

  </output>

</binding>

<service name="FOOSAMPLEService">

 <port name="SimplePort" binding="wsdlns:SimpleBinding">

  <soap:address location="http://carlos:8080/FooSample/FooSample.asp"/>

 </port>

</service>

</definitions>

  以下是該執行個體文檔的總述:稍後我将詳細讨論每一部分的細節。

  第一行申明該文檔是XML。盡管這并不是必需的,但它有助于XML解析器決定是否解析WSDL檔案或隻是報錯。第二行是WSDL文檔的根元素:<definitions>。一些屬性附屬于根元素,就像<schema>子元素對于<types>元素。

  <types>元素包含了Types欄。如果沒有需要聲明的資料類型,這欄可以預設。在WSDL範例中,沒有應用程式特定的types聲明,但我仍然使用了Types欄,隻是為了聲明schema namespaces。

  <message>元素包含了Messages欄。如果我們把操作看作函數,<message>元素定義了那個函數的參數。<message>元素中的每個<part>子元素都和某個參數相符。輸入參數在<message>元素中定義,與輸出參數相隔離--輸出參數有自己的<message>元素。兼作輸入、輸出的參數在輸入輸出的<message>元素中有它們相應的<part>元素。輸出<message>元素以"Response"結尾,就像以前所用的"fooResponse"。每個<part>元素都有名字和類型屬性,就像函數的參數有參數名和參數類型。

  用于交換文檔時,WSDL允許使用<message>元素來描述交換的文檔。

  <part>元素的類型可以是XSD基類型,也可以是SOAP定義類型(soapenc)、WSDL定義類型(wsdl)或是Types欄定義的類型。

  一個PortTypes欄中,可以有零個、單個或多個<portType>元素。由于抽象PortType定義可以放置在分開的檔案中,在某個WSDL檔案中沒有<portType>元素是可能的。上面的例子裡隻是用了一個<portType>元素。而一個<portType>元素可在<operation>元素中定義一個或是多個操作。示例僅使用了一個名為"foo"的<operation>元素。這和某個函數名相同。<operation>元素可以有一個、兩個、三個子元素:<input>, <output> 和<fault>元素。每個<input>和<output>元素中的消息都引用Message欄中的相關的<message>元素。這樣,示例中的整個<portType>元素就和以下的C函數等效:

 

 int foo(int arg);

  這個例子足見XML和C相比要冗長的多。(包括<message>元素,XML在示例中共使用了12行代碼來表達相同的單行函數聲明。)

  Bindings欄可以有零個、一個或者多個<binding>元素。它的意圖是制定每個<operation>通過網絡調用和回應。Services欄同樣可以有零個、一個、多個<service>元素。它還包含了<port>元素,每個<port>元素引用一個Bindings欄裡的<binding>元素。Bindings和Services欄都包含WSDL文檔。