Geeks With Blogs
Dot Net Dunk Wandering in the land of .NET

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 />
</mydoc
>
(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" />
</ns0:mydoc>

(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" />
</ns0:mydoc>

(No default namespace, but ns0 and ns1 both map to uri:my-namespace and hence have identical meaning)
 
and
 
<ns0:mydoc xmlns:ns0="uri:my-namespace">
   <subnode xmlns="uri:my-namespace" />
</ns0:mydoc>

(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.

ElementFormDefault
As 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:complexType>
        
<xs:sequence>
           
<xs:element name="subnode" maxOccurs="1" />
         </
xs:sequence>
      </
xs:complexType>
   </xs:element>
</xs:schema>

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">
  <
subnode/>
</ns0:mydoc>

and

 
<ns0:mydoc xmlns:ns0="uri:my-namespace">
   <
ns1:subnode xmlns:ns1=""/>
</ns0:mydoc>
(An explicitly empty prefixed namespace definition)
 
and

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

(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 :-)
Posted on Wednesday, October 20, 2004 6:08 PM BizTalk Server , C#/.NET Development , Nuggets | Back to top


Comments on this post: XML Namespaces, Prefixes, and elementFormDefault

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
The topic is extremely helpful.
Left by Maria Brag on Aug 21, 2005 12:49 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Can't agree more. Finally become enlightened after all the confusion. Thank you.
Left by Sammy on Jan 10, 2006 12:04 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
I'm not finding this to be quite the case. In BT2006, the mapping engine differentiates between

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

and

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

Running the first through a map produces no output while running the second through a map does (with data added to the file, of course)
Left by Jim on Jun 15, 2006 12:07 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
I just got what I wanted from this short article. Thank you very much
Left by Ramesh Gurunathan on Jan 05, 2007 9:58 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Finally a little enlightenment on ElementFormDefault.

Thank you!
Left by João C. Morais on Aug 02, 2007 9:10 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Nice and concise explanation that finally cleared up for me on what elementFormDefault really meant. thanks.
Left by Tarun on Sep 19, 2007 3:50 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Dude.. thank you very much!
Left by EJ on Jan 14, 2008 3:38 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Thanks for this very usefull explanation.
Left by Phil on Apr 09, 2008 8:02 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Hi!

Quite goog and clear article, but I don't understand the point of having a different namespace in the subnodes, surely they belong to the parent namespace in these examples???

<ns0:mydoc xmlns:ns0="uri:my-namespace">
<ns1:subnode xmlns:ns1=""/>
</ns0:mydoc>
(An explicitly empty prefixed namespace definition)

and

<ns0:mydoc xmlns:ns0="uri:my-namespace">
<subnode xmlns=""/>
</ns0:mydoc>
(An explicitly empty default namespace definition for the sub node, and its child nodes if it had any)

Thanks.
Left by Monica Ferrero on Apr 16, 2008 6:33 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Hi Monica,

The two examples you highlight have child nodes in the empty namespace (""). The empty namespace does not mean "use the parent namespace", so here you have a parent node called mydoc, in the namespace "uri:my-namespace", and you have a child node called subnode in the empty namespace ("").

Have I explained it ok?
Left by Duncan Millard on Apr 16, 2008 7:00 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Hi,

As per your saying that using of namespace prefix does not affect but it is affecting in my case.
I tried to make an attribute with namespace "xmlns:xsi"..... Oops what i saw in my output that my this attribute is splitted into two parts xmlns:ns0="xmlns" and ns0:xsi="http://www.w3.org/2001/XMLSchema-instance". I used the xslt like this

<xsl:attribute name="xmlns:xsi" namespace="xmlns">
<xsl:text>http://www.w3.org/2001/XMLSchema-instance</xsl:text>
</xsl:attribute>

The output getting like above is not validating against Schema....
Can anybody let me where i am making mistake...

Regards,
Mona
Left by Mona on Jun 25, 2008 1:50 PM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Perfectly explained (and still valid after almost 5 years). Helped me a lot! Thx
Left by Martin on Jul 14, 2009 8:00 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Thanks Martin!
Left by Duncan Millard on Jul 14, 2009 10:01 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
I agree with Martin. This perfectly bridges the gap between the 101 tutorials and the dense reference material of W3C and Microsoft etc. Thanks for taking the time to cover the subject so clearly.
Left by David on Aug 06, 2009 3:05 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
Thanks. I didn't know why ns1 was automatically inserted as prefix for namespaces. This article expalins it.
Left by Aruna on Dec 24, 2009 4:56 AM

# re: XML Namespaces, Prefixes, and elementFormDefault
Requesting Gravatar...
I have my xml something in below format.
<ns0:mydoc xmlns:ns0="uri:my-namespace">
<subnode />
</ns0:mydoc>
I want it to be as below.
<mydoc xmlns="uri:my-namespace">
<subnode />
</mydoc>
Can you please suggest on what can be done? i have set the elementformdefault property to unqualified. but not able to remove the ns0: from 1st line.
Left by smriti on May 10, 2012 10:04 AM

Your comment:
 (will show your gravatar)
 


Copyright © Duncan Millard | Powered by: GeeksWithBlogs.net | Join free