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.
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: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 🙂