From w3c school
SOAP 構模組化塊
一條 SOAP 消息就是一個普通的 XML 文檔,包含下列元素:
必需的 Envelope 元素,可把此 XML 文檔辨別為一條 SOAP 消息
可選的 Header 元素,包含頭部資訊
必需的 Body 元素,包含所有的調用和響應資訊
可選的 Fault 元素,提供有關在處理此消息所發生錯誤的資訊
所有以上的元素均被聲明于針對 SOAP 封裝的預設命名空間中:
http://www.w3.org/2001/12/soap-envelope
以及針對 SOAP 編碼和資料類型的預設命名空間:
http://www.w3.org/2001/12/soap-encoding
envelope:信封,包裝,塑膠封皮
文法規則
這裡是一些重要的文法規則:
SOAP 消息必須用 XML 來編碼
SOAP 消息必須使用 SOAP Envelope 命名空間
SOAP 消息必須使用 SOAP Encoding 命名空間
SOAP 消息不能包含 DTD 引用
SOAP 消息不能包含 XML 處理指令
SOAP 消息的基本結構
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
...
...
</soap:Header>
<soap:Body>
...
...
<soap:Fault>
...
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
SOAP Envelope 元素
必需的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。它可把 XML 文檔定義為 SOAP 消息。
請注意 xmlns:soap 命名空間的使用。它的值應當始終是:
http://www.w3.org///soap-envelope
并且它可把封裝定義為 SOAP 封裝:
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
...
Message information goes here
...
</soap:Envelope>
xmlns:soap 命名空間
SOAP 消息必須擁有與命名空間 “http://www.w3.org/2001/12/soap-envelope” 相關聯的一個 Envelope 元素。
如果使用了不同的命名空間,應用程式會發生錯誤,并抛棄此消息。
encodingStyle 屬性
SOAP 的 encodingStyle 屬性用于定義在文檔中使用的資料類型。此屬性可出現在任何 SOAP 元素中,并會被應用到元素的内容及元素的所有子元素上。SOAP 消息沒有預設的編碼方式。
文法
執行個體
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
...
Message information goes here
...
</soap:Envelope>
SOAP Header 元素
可選的 SOAP Header 元素可包含有關 SOAP 消息的應用程式專用資訊(比如認證、支付等)。如果 Header 元素被提供,則它必須是 Envelope 元素的第一個子元素。
注釋:所有 Header 元素的直接子元素必須是合格的命名空間。
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
<m:Trans
xmlns:m="http://www.w3school.com.cn/transaction/"
soap:mustUnderstand="1"></m:Trans>
</soap:Header>
...
...
</soap:Envelope>
上面的例子包含了一個帶有一個 “Trans” 元素的頭部,它的值是 234,此元素的 “mustUnderstand” 屬性的值是 “1”。
SOAP 在預設的命名空間中 (“http://www.w3.org/2001/12/soap-envelope“) 定義了三個屬性。這三個屬性是:actor、 mustUnderstand 以及 encodingStyle。這些被定義在 SOAP 頭部的屬性可定義容器如何對 SOAP 消息進行處理。
actor 屬性
通過沿着消息路徑經過不同的端點,SOAP 消息可從某個發送者傳播到某個接收者。并非 SOAP 消息的所有部分均打算傳送到 SOAP 消息的最終端點,不過,另一個方面,也許打算傳送給消息路徑上的一個或多個端點。
SOAP 的 actor 屬性可被用于将 Header 元素尋址到一個特定的端點。
執行個體
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
<m:Trans
xmlns:m="http://www.w3school.com.cn/transaction/"
soap:actor="http://www.w3school.com.cn/appml/">
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
mustUnderstand 屬性
SOAP 的 mustUnderstand 屬性可用于辨別标題項對于要對其進行處理的接收者來說是強制的還是可選的。
假如您向 Header 元素的某個子元素添加了 “mustUnderstand=”1”,則它可訓示處理此頭部的接收者必須認可此元素。假如此接收者無法認可此元素,則在處理此頭部時必須失效。
文法
執行個體
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
<m:Trans
xmlns:m="http://www.w3school.com.cn/transaction/"
soap:mustUnderstand="1">
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
SOAP Body 元素
必需的 SOAP Body 元素可包含打算傳送到消息最終端點的實際 SOAP 消息。
SOAP Body 元素的直接子元素可以是合格的命名空間。SOAP 在預設的命名空間中(“http://www.w3.org/2001/12/soap-envelope“)定義了 Body 元素内部的一個元素。即 SOAP 的 Fault 元素,用于訓示錯誤消息。
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body>
<m:GetPrice xmlns:m="http://www.w3school.com.cn/prices">
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
上面的例子請求蘋果的價格。請注意,上面的 m:GetPrice 和 Item 元素是應用程式專用的元素。它們并不是 SOAP 标準的一部分。
而一個 SOAP 響應應該類似這樣:
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body>
<m:GetPriceResponse xmlns:m="http://www.w3school.com.cn/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>
SOAP Fault 元素
可選的 SOAP Fault 元素用于訓示錯誤消息。
如果已提供了 Fault 元素,則它必須是 Body 元素的子元素。在一條 SOAP 消息中,Fault 元素隻能出現一次。SOAP Fault 代碼在下面定義的 faultcode 值必須用于描述錯誤時的 faultcode 元素中:
versionmismatch: SOAP Envelope 元素的無效命名空間被發現
mustunderstand: Header 元素的一個直接子元素(帶有設定為 “1” 的 mustUnderstand 屬性)無法被了解
client: 消息被不正确地構成,或包含了不正确的資訊。
server: 伺服器有問題,是以無法處理進行下去。
ok, soap文法就是這樣。
(轉自 w3c school)