Dot Net Dunk

Wandering in the land of .NET
posts - 42, comments - 21, trackbacks - 165

My Links

News

Archives

Post Categories

Blog Roll

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 />
</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 :-)

Print | posted on Wednesday, October 20, 2004 6:08 PM | Filed Under [ BizTalk Server C#/.NET Development Nuggets ]


Feedback

# re: XML Namespaces, Prefixes, and elementFormDefault

The topic is extremely helpful. 8/21/2005 12:49 PM | Maria Brag

# re: XML Namespaces, Prefixes, and elementFormDefault

Can't agree more. Finally become enlightened after all the confusion. Thank you. 1/10/2006 12:04 PM | Sammy

# re: XML Namespaces, Prefixes, and elementFormDefault

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)
6/15/2006 12:07 PM | Jim

# re: XML Namespaces, Prefixes, and elementFormDefault

I just got what I wanted from this short article. Thank you very much 1/5/2007 9:58 AM | Ramesh Gurunathan

# re: XML Namespaces, Prefixes, and elementFormDefault

Finally a little enlightenment on ElementFormDefault.

Thank you! 8/2/2007 9:10 AM | João C. Morais

# re: XML Namespaces, Prefixes, and elementFormDefault

Nice and concise explanation that finally cleared up for me on what elementFormDefault really meant. thanks. 9/19/2007 3:50 AM | Tarun

# re: XML Namespaces, Prefixes, and elementFormDefault

Dude.. thank you very much! 1/14/2008 3:38 PM | EJ

# re: XML Namespaces, Prefixes, and elementFormDefault

Thanks for this very usefull explanation. 4/9/2008 8:02 AM | Phil

# re: XML Namespaces, Prefixes, and elementFormDefault

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. 4/16/2008 6:33 PM | Monica Ferrero

# re: XML Namespaces, Prefixes, and elementFormDefault

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? 4/16/2008 7:00 PM | Duncan Millard

# re: XML Namespaces, Prefixes, and elementFormDefault

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 6/25/2008 1:50 PM | Mona

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 1 and type the answer here:

Powered by: