强制使用的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。
SOAP Envelope 元素
必需的 SOAP 的 Envelope 元素是 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"> ... Message information goes here ... </soap:Envelope>
xmlns:soap 命名空间
SOAP 消息必须拥有与命名空间 "http://www.w3.org/2001/12/soap-envelope" 相关联的一个 Envelope 元素。
如果使用了不同的命名空间,应用程序会发生错误,并抛弃此消息。
encodingStyle 属性
SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。
SOAP 消息没有默认的编码方式。
语法
soap:encodingStyle="URI"
实例
<?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 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)。
如果 Header 元素被提供,则它必须是 Envelope 元素的第一个子元素。
注意: 所有 Header 元素的直接子元素必须是合格的命名空间。
< 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.w3schools.com/transaction/"
soap:mustUnderstand="1">234
< /m:Trans>
< /soap:Header>
...
...
< /soap:Envelope>
上面的例子包含了一个带有一个 "Trans" 元素的头部,它的值是 234,此元素的 "mustUnderstand" 属性的值是 "1"。
SOAP 在默认的命名空间中 ("http://www.w3.org/2001/12/soap-envelope") 定义了三个属性。
这三个属性是:actor、 mustUnderstand 以及 encodingStyle。这些被定义在 SOAP 头部的属性可定义容器如何对 SOAP 消息进行处理。
mustUnderstand 属性
SOAP 的 mustUnderstand 属性可用于标识标题项对于要对其进行处理的接收者来说是强制的还是可选的。
假如您向 Header 元素的某个子元素添加了 "mustUnderstand="1",则它可指示处理此头部的接收者必须认可此元素。假如此接收者无法认可此元素,则在处理此头部时必须失效。
语法
实例
< 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.w3schools.com/transaction/"
soap:mustUnderstand="1">234
< /m:Trans>
< /soap:Header>
...
...
< /soap:Envelope>
actor 属性
通过沿着消息路径经过不同的端点,SOAP 消息可从某个发送者传播到某个接收者。并非 SOAP 消息的所有部分均打算传送到 SOAP 消息的最终端点,不过,另一个方面,也许打算传送给消息路径上的一个或多个端点。
SOAP 的 actor 属性可被用于将 Header 元素寻址到一个特定的端点。
语法
实例
< 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.w3schools.com/transaction/"
soap:actor="http://www.w3schools.com/appml/">234
< /m:Trans>
< /soap:Header>
...
...
</soap:Envelope>
encodingStyle 属性
SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。
SOAP 消息没有默认的编码方式。
语法
soap:encodingStyle="URI"
强制使用的 SOAP Body 元素包含实际的 SOAP 消息。
SOAP Body 元素
必需的 SOAP Body 元素可包含打算传送到消息最终端点的实际 SOAP 消息。
SOAP Body 元素的直接子元素可以是合格的命名空间。
实例
< 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.w3schools.com/prices">
<m:Item>Apples</m:Item>
</m:GetPrice>
< /soap:Body>
< /soap:Envelope>
上面的例子请求苹果的价格。请注意,上面的 m:GetPrice 和 Item 元素是应用程序专用的元素。它们并不是 SOAP 标准的一部分。
而一个 SOAP 响应应该类似这样:
< 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.w3schools.com/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
< /soap:Body>
< /soap:Envelope>
SOAP Fault 元素用于存留 SOAP 消息的错误和状态信息。
SOAP Fault 元素
可选的 SOAP Fault 元素用于指示错误消息。
如果已提供了 Fault 元素,则它必须是 Body 元素的子元素。在一条 SOAP 消息中,Fault 元素只能出现一次。
SOAP 的 Fault 元素拥有下列子元素:
子元素 | 描述 |
---|---|
<faultcode> | 供识别故障的代码 |
<faultstring> | 可供人阅读的有关故障的说明 |
<faultactor> | 有关是谁引发故障的信息 |
<detail> | 存留涉及 Body 元素的应用程序专用错误信息 |
SOAP Fault 代码
在下面定义的 faultcode 值必须用于描述错误时的 faultcode 元素中:
错误 | 描述 |
---|---|
VersionMismatch | SOAP Envelope 元素的无效命名空间被发现 |
MustUnderstand | Header 元素的一个直接子元素(带有设置为 "1" 的 mustUnderstand 属性)无法被理解。 |
Client | 消息被不正确地构成,或包含了不正确的信息。 |
Server | 服务器有问题,因此无法处理进行下去。 |