Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท XML and SOAP Messages

XML Namespaces in SOAP

Understand XML namespaces, prefixes, default namespaces and why SOAP depends on them, then write code that resolves prefixes to namespace URIs and spots version mismatches.

Look at any SOAP message and you will see prefixes everywhere: soap:Envelope, m:GetPrice, wsse:Security. These prefixes are XML namespaces, and they are the mechanism that lets one message safely mix elements from the SOAP standard, from WS-Security and from your own business schema. If namespaces confuse you, SOAP errors will confuse you too, so this lesson takes them slowly.

The problem namespaces solve

Imagine two standards both define an element called Body. One means the body of a SOAP message, the other the body of an HTML page. In one document, the processor cannot tell them apart. A namespace is a unique name that qualifies an element so equal local names do not collide. The name is a URI, such as http://schemas.xmlsoap.org/soap/envelope/. It is only an identifier: nothing is downloaded from it, and it need not be a working web address.

Declaring and using prefixes

A namespace is declared with an xmlns:prefix attribute, and the prefix is then written before the element name, separated by a colon.

XML
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:shop="urn:example:shop">
  <soap:Body>
    <shop:GetPrice>
      <shop:product>tea</shop:product>
    </shop:GetPrice>
  </soap:Body>
</soap:Envelope>

The full name of the first element is "Envelope in the namespace http://schemas.xmlsoap.org/soap/envelope/". The prefix soap is just a nickname: another message may call the same namespace env or s, and it means the same thing. This is the number one rule: compare namespace URIs, never prefixes.

A declaration is in scope for the element that carries it and all its descendants, and an inner element can redeclare a prefix.

The default namespace

xmlns="..." with no prefix sets the namespace for all unprefixed element names inside:

XML
<GetPrice xmlns="urn:example:shop">
  <product>tea</product>
</GetPrice>

Here GetPrice and product both belong to urn:example:shop. Careful: the default namespace does not apply to unprefixed attributes. Attributes with no prefix have no namespace.

Mixing is common in SOAP: the envelope uses a prefix, and the body content uses a default namespace declared on the operation element.

Resolving names in code

A parser must walk the tree with a table of prefixes and resolve every name. The next program does it: first the compact parser from the last lesson (shortened), then a resolver that gives each element { ns, local }.

Node.js Resolve prefixes to namespace URIs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Look at the output: Envelope and Body are in the SOAP namespace, while GetPrice and product are in urn:example:shop, even though product has no prefix, because the default namespace flows down.

Prefixes do not matter, URIs do

Two documents that look different can be the same message. The program below proves it by resolving both and comparing {namespace, local name} pairs.

Node.js Different prefixes, same message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Document c uses the same prefix as a but a different namespace, so it is a different message: a SOAP 1.2 envelope. This is why code that does xml.includes('soap:Envelope') is fragile.

Which namespaces appear in SOAP?

Namespace URI Prefix by habit What it is
http://schemas.xmlsoap.org/soap/envelope/ soap, soapenv, s SOAP 1.1 envelope
http://www.w3.org/2003/05/soap-envelope env, soap12 SOAP 1.2 envelope
http://www.w3.org/2001/XMLSchema xsd, xs XML Schema types (xsd:string)
http://www.w3.org/2001/XMLSchema-instance xsi xsi:type, xsi:nil
http://schemas.xmlsoap.org/wsdl/ wsdl WSDL 1.1
http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd wsse WS-Security
http://www.w3.org/2005/08/addressing wsa WS-Addressing
urn:yourcompany:orders anything your service's namespace

Every service defines its own target namespace. All its elements live there, which is how a client tells your Order apart from someone else's. Choose a URI you control (a URL on your domain, or a urn:) and keep it stable: changing it is a breaking change for every client.

Namespace mistakes show up as faults like "Cannot find operation for element {urn:shop}getPrice" or "unexpected element". When debugging, print the resolved local name of the first body element and compare it with the WSDL's target namespace, letter by letter, including a trailing slash.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What identifies the namespace of an element?

    The prefix is only a local nickname. The namespace is the URI it is bound to.
  2. 2. Is the URI of a namespace downloaded when a message is processed?

    A namespace URI is a unique name. Processors do not fetch it.
  3. 3. Given <a xmlns="urn:x"><b/></a>, which namespace is element b in?

    The default namespace applies to all unprefixed descendant elements.
  4. 4. A message uses soap:Envelope with the namespace http://www.w3.org/2003/05/soap-envelope. Which SOAP version is it?

    That namespace belongs to SOAP 1.2. SOAP 1.1 uses http://schemas.xmlsoap.org/soap/envelope/.

Summary

  • A namespace is a URI that qualifies element names so different vocabularies can share one document.
  • Prefixes are nicknames; always compare resolved namespace URIs.
  • xmlns sets a default namespace for unprefixed elements but not for attributes.
  • SOAP mixes the envelope namespace, XML Schema, WS-* namespaces and the service's own target namespace.
  • Keep your target namespace stable; changing it breaks clients.

Next: The SOAP Envelope, Header and Body