XML technologies / XML / Python & XML / Python, web services and SOAP / The emerging SOAP standard

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

Understanding SOAP helps you use implementations effectively and, more importantly, use SOAP as a general XML messaging medium. SOAP is still under development but intended to become a W3C Recommendation. At the time of writing, the latest specification is a W3C paper (Note from the data2type editors: Since 2007 SOAP has been a W3C Recommendation). W3C members from DevelopMentor, IBM, UserLand, Lotus Development, Microsoft and other companies develop SOAP.

SOAP is an XML-based protocol built on three concepts:

  1. An envelope describing a message and how to process it.

  2. Encoding requirements describing message data types.

  3. Remote procedure call conventions enabling distributed method calls.

SOAP messages

In its simplest form, SOAP sends a message over HTTP to a SOAP server. The server implements functionality and returns a SOAP response. This uses HTTP’s request/response mechanism: the original message may contain a method call and parameters, and the reply its return values.

A SOAP request might look like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetLocalTemperature xmlns:m="http://localhost/temperApp">
      <zipcode>90872</zipcode>
    </m:GetLocalTemperature>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This message travels over HTTP to a URI capable of interpreting and answering it. The returned SOAP unit answers the request Get-LocalTemperature.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  <SOAP-ENV:Body>
    <m:GetLocalTemperatureResponse xmlns:m="http://localhost/temperApp">
      <Farenheit>59</Farenheit>
    </m:GetLocalTemperatureResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This response returns the method call’s result to the caller in a SOAP package.

Exchanging SOAP messages

SOAP places no restrictions on network transport. Implementations may exploit any special features of HTTP, SMTP or a future medium.

It does, however, define a message path (Message Path). This lets intermediaries process a package on its way to the final destination. Rather than merely hopping between points, the concept models routing. Intelligent networks can handle SOAP packages and send them where needed. Distributed routing, rather than forcing everything through a central server, greatly improves scalability and traffic management.

SOAP intermediaries must perform these three steps in order:

  1. Identify every message part intended for the processor: the application must understand which parts concern its operation and which do not.

  2. Decide whether all processing required by the message is possible. If not, ignore the message.

  3. If not the final destination, remove processed parts before forwarding the message to the next point.

Some middleware and routing applications have no message parts addressed specifically to them. They simply inspect the destination URI or SoapAction value and forward the package unchanged.

Encoding SOAP messages

SOAP requires every message to be XML. Elements and attributes also use namespaces, which all SOAP applications must understand. Messages with incorrect namespaces must not be processed. Two namespaces are defined: for envelopes (envelopes), the correct namespace is "http://schemas.xmlsoap.org/soap/envelope/", and for serialization the correct namespace is "http://schemas.xmlsoap.org/soap/encoding/".

These namespaces combine with local names in element and attribute names according to the W3C document Namespaces. Interestingly, despite their XML origins, SOAP messages must contain neither a document type declaration nor processing instructions.

Creating SOAP envelopes

A complete message has three main parts. The outermost is the envelope (Envelope). Inside it is the header (Header), holding routing information or other metadata not intended for applications. Headers may change temporarily during routing or transport provided the original message state is preserved at its destination. The application-specific portion is in the SOAP body (Body).

SOAP package requirements

Consider physical shipping: the envelope is the container, header data may be added or removed at transit points, and the body is the protected goods only the recipient may handle.

Thus envelope and body are required, while the header is optional. Additional constraints apply:

  1. An envelope’s element name must always be Envelope. The element Envelope may have namespace declarations and other informative attributes, but any such attributes must be namespace-qualified. Messages must have an element Envelope marked with namespace "http://schemas.xmlsoap.org/soap/envelope/". Messages outside that namespace context must be discarded.

  2. A header element must always be named Header. Its direct children must all be namespace-qualified.

  3. A body element must always be named Body. The element Body must be a direct child of Envelope and must immediately follow the element Header if a header exists.

SOAP encoding style

SOAP permits different serialization rules. The attribute encodingStyle identifies the techniques used. SOAP defines serialization rules in the specification and uses URI "http://schemas.xmlsoap.org/soap/encoding/" to signal this encoding style.

Using SOAP headers

Optional header data extends messages. The header data may be seen only by intermediaries and middleware, never by endpoint applications. Nothing, however, prohibits endpoints from using headers.

Header entries must follow rules. First, use fully qualified element names within a namespace URI context. Second, the SOAP attribute encodingStyle may specify their encoding. Third, SOAP attributes mustUnderstand and actor may specify processing directions.

SOAP attribute actor
The SOAP attribute actor identifies the recipient of an element header using a URI.

SOAP attribute mustUnderstand
The attribute mustUnderstand tells an application whether it must process the element’s information. The element mustUnderstand may have value 1 or 0, with 1 requiring the application to understand the element. An absent attribute mustUnderstand equals one with value 0 or otherwise signifies a false condition.

SOAP body elements

The element Body is the main part handled by an endpoint application and carries the package’s actual payload.

Children of the element Body are called body entries (Body Entries). Each entry in SOAP element Body is encoded independently and needs a URI and local name. The attribute encodingStyle may specify an entry’s encoding.

SOAP error messages and fault elements

SOAP uses the element Fault to return error conditions to the caller. It may convey any error relevant to the application.

Fault elements

A Fault element may have these four children:

faultcode
The element faultcode is required in Fault elements and provides a numeric code for easier error management. SOAP defines codes discussed below under “Fault codes”.

faultstring
The element faultstring is required in Fault elements and may contain any appropriate error description.

faultactor
The element faultactor identifies the agent causing an error along a message path. If present, it indicates the error’s origin. An intermediary causing the error must identify itself in the element faultactor. The value of an element faultactor is a URI.

detail
The element detail associates application-specific information with the XML payload in the element Body. A business-logic error in a distributed SOAP application puts details in the element detail. Conversely, a routing intermediary’s error does not use the element detail for this information. Like the element Body, the element detail allows detail entries (detail entries) as direct children.

Fault codes

SOAP defines four error conditions requiring the following codes. They occupy a space defined by the URI prefix "http://schemas.xmlsoap.org/soap/envelope/". The specification expects developers to extend the codes. The defaults are:

VersionMismatch
Used when an invalid namespace is supplied for SOAP Envelope.

MustUnderstand
Used when an application cannot understand or process an element whose attribute mustUnderstand is set to 1.

Client
Used for malformed messages or missing information required for success.

Server
Used when fundamental server-side reasons prevent processing. Your call to GetLocalTemperature might be correctly formatted, but the server could be offline. The application may retry later.

Client and server error classes should be extensible, letting programmers define errors such as Client.AccessDenied or Server.Unavailable. The complete URI for Client.AccessDenied is "http://schemas.xmlsoap.org/soap/envelope/Client.AccessDenied".

Errors outside the SOAP specification may use URIs with another prefix.

SOAP encoding techniques

SOAP encodings define data-type formats within packages. When using SOAP for Remote Procedure Calls (RPC) between applications, application-specific data must travel between parties. Applications must understand its types, distinguishing lists from strings and numbers from letters.

SOAP encoding distinguishes two kinds of data: simple scalar types (dog = "foo") and compound types (dog = {"foo" : "bar", "bar" : "foo"}). Its namespace URI is "http://schemas.xmlsoap.org/soap/encoding/".

Other encodings are allowed, but interoperability is easiest when applications use the same one.

SOAP encoding rules

SOAP has nine golden serialization rules governing simple and complex data types and their representation. The following practical guidelines explore and illustrate them.

  1. All data values must appear as element content, not attributes, as here:
    <specialSymbol>DatenWerte</specialSymbol>
    but not:
    <specialSymbols symbol1="DatenWert1" symbol2="DatenWert2"/>

  2. An element containing a data value must satisfy one of these conditions:

    • Have an attribute xsi:type.

    • Be contained in an element with the attribute SOAP-ENC:arrayType.

    • Have a type inferable from a schema.

  3. Simple values are character data without children. Their types must be referenced by the specification XML Schema.

  4. Compound values are element sequences. Accessors are elements with appropriate names. Names must be qualified unless local to their containing types.

  5. Simple or compound multireference values are independent elements with a local ID attribute, of XML’s ID type and unique in each document instance. Every accessor must have an attribute named href pointing to a URI fragment identifier referencing the element.

  6. Strings and byte arrays should be simple multireference types, though rules allow efficient representation of common cases.

  7. Multiple references to one value may be encoded separately only if the resulting XML instance’s meaning remains unchanged.

  8. Arrays are compound values of type SOAP-ENC:Array or a derived type. SOAP arrays may be multidimensional, with the rightmost index varying first. They require attribute SOAP-ENC:arrayType specifying element type and dimension. Its simplest form is:
    arrayTypeValue: array-type array-size
    Here <array-type> is an XML Schema type and <array-size> an integer specifying array size. Multidimensional arrays complicate matters: <array-size> is then a comma-separated integer list.

  9. Null values need no accessor, but may be present with an accessor containing the value 1 in the attribute xsi:null.

These rules may look complex, but learning about types demystifies them. SOAP APIs, hopefully all of them, perform this strict typing for you rather than requiring manual work.

Simple types

SOAP adopts the types from XML Schema Part 2: Datatypes. Instead of reinventing the wheel, its authors build on XML Schema’s work.

Established types make data encoding much easier to understand than the nine rules alone. For example:

<element name="Vorname" type="xsd:string"/>
<element name="Nachname" type="xsd:string"/>
<element name="Adresse1" type="xsd:string"/>
<element name="Ort" type="xsd:string"/>
<element name="Land" type="xsd:string"/>
<element name="PLZ" type="int"/>
<element name="Betrag" type="float"/>

Compound types

SOAP distinguishes two principal compound types: Structs and Arrays. A struct has named parts accessed through those names. An array is an ordered list accessed by integer index.

SOAP over HTTP

SOAP naturally fits HTTP: RPC-style request/response transactions suit HTTP’s protocol perfectly. When sending SOAP over HTTP, the content type must be text/xml.

The SOAPAction header

The HTTP request header SOAPAction specifies the SOAP request’s “intent”. Clients must supply it. Its value is a URI, with no restriction on what that URI represents.

SOAP HTTP responses

SOAP over HTTP combines HTTP response codes with equivalent SOAP outcomes. Even if HTTP itself succeeds, any server-side SOAP processing error requires “HTTP 500 Internal Server Error”. Unlike ordinary HTTP, where this follows an abnormal CGI or ASP termination, the SOAP server may run normally yet return HTTP 500 when the message’s logical execution fails.

SOAP for RPC

RPC use differs little from other SOAP use. Request/response semantics remain: a call is an envelope carrying a method name and parameters; its reply is a return value or error status, also inside an envelope.

SOAP RPC method calls and return values are stored in the body.

  

 

 

 

back to top