Close this search box.

XML Namespaces, Prefixes, and elementFormDefault

Note: The GeeksWithBlogs editor seems to keep really messing with the formatting / XML of this post. If you spot any errors, please hit the Contact Me link above and I’ll try to wrestle it into submission again 🙂

This entry may be useful to people confused by BizTalk’s inclination to “ns0” at the drop of a hat 🙂

Someone asked me today why their XML document wasn’t validating. They thought that it was because the schema was expecting messages with a default namespace of “uri:my-namespace” but the document they were validating instead defined all the nodes using a prefix of “ns0”, with ns0 mapping to uri:my-namespace.

One of the biggest misunderstandings in XML is namespaces. There’s absolutely no difference to an xml parser or to the interpretation of a document whether you use a prefix on every node, or a default namespace. Prefixes are only a syntactic abbreviation. In other words:

<mydoc xmlns="uri:my-namespace">
    <subnode />

(Default namespace flows into the child – there is no prefix, so it uses the default namespace)

is identical to:

  <ns0:mydoc xmlns:ns0="uri:my-namespace">
    <ns0:subnode xmlns:ns1="uri:my-namespace" />

(The prefix ns0 maps to uri:my-namespace, and both nodes are marked with ns0)

and to:

<ns0:mydoc xmlns:ns0="uri:my-namespace">
    <ns1:subnode xmlns:ns1="uri:my-namespace" />

(No default namespace, but ns0 and ns1 both map to uri:my-namespace and hence have identical meaning)

<ns0:mydoc xmlns:ns0="uri:my-namespace">
   <subnode xmlns="uri:my-namespace" />

(ns0 is defined on the parent, but we then set a default namespace for this node – and any children, if it had any)

In all of the cases above, the namespace of subNode is uri:my-namespace.

It turned out in the end that the problem was that the schema defined the namespace as “uri:my-namespace” but the result document was coming back with a namespace of “uri:my-namespace/” – the trailing slash being all important. Remember folks, namespaces are just strings – a trailing slash may not make a difference to most Internet browsers, but they do in XML Schema.

ElementFormDefaultAs a bonus, a little note on elementFormDefault. 

If in your schema you use elementFormDefault=”qualified” then you can validate all of the documents above with a schema similar to (pseudo-code): 

<xs:schema targetNamespace="uri:my-namespace" elementFormDefault="qualified">
   <xs:element name="myDoc">
            <xs:element name="subnode" maxOccurs="1" />

If you changed the elementFormDefault to “unqualified” then instead it would only look for the root element to be in the namespace, and child elements would be unqualified (in the empty namespace):

<ns0:mydoc xmlns:ns0="uri:my-namespace">


<ns0:mydoc xmlns:ns0="uri:my-namespace">
   <ns1:subnode xmlns:ns1=""/>

(An explicitly empty prefixed namespace definition)

<ns0:mydoc xmlns:ns0="uri:my-namespace">
   <subnode xmlns=""/>

(An explicitly empty default namespace definition for the sub node, and its child nodes if it had any)I was lucky enough to learn about Namespaces on a Developmentor training course run by Aaron Skonnard ages ago so thanks to him for the foundations of this entry 🙂

This article is part of the GWB Archives. Original Author: Duncan Millard

Related Posts