XML Namespaces


I've been working with XML for several years now but I've never been entirely sure that I fully understand all there is to know about namespaces. Of course namespaces are central to how document instances are defined by BizTalk, so I certainly had some understanding. However, it's quite easy to just "get by" without a complete understanding because of the inteli-sense and other helpful features provided by tools like Visual Studio and XML Spy. If I'd had only notepad in which to create the XSDs and XML documents then I'm sure I would have been stuck.
Anyhow, this has been bothering me for a while, so today I decided I was going to learn all there is to know about XML namespaces - and make it stick! This learning involved reading sections from the excellent book "The XML Schema Companion" by Neil Bradley, and otherwise messing around with XSDs and XML documents in Visual Studio.
Key Points
  • It is possible to create an XSD that does not define a namespace. Such schemas can be used to validate unqualified XML documents. Below I have pasted a simple XSD that does not define a namespace:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="RootNode">
<xs:documentation>Comment describing your root element</xs:documentation>
<xs:element name="Name"/>
<xs:element name="Age"/>
I then used XML spy to create an XML document instance from the above XSD, this is what it came up with:
<?xml version="1.0" encoding="UTF-8"?>
<RootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\Rob\LearnXMLNamespaces\Person.xsd">
Note, the root node has the attribute "xmlns:xsi", this is simply defining a prefix for a namespace - it is not setting the document level namespace. In effect it is saying that elements, attributes etc from the namespace http://www.w3.org/2001/XMLSchema-instance can be used by prefixing XSI, however where there is no prefix then the element, attribute etc does not belong to a namespace - it is unqualified. The attribute noNamespaceSchemaLocation is interesting. This gives the URL reference to the XSD that will be used when validating the XML document. It also infers that the XSD does not define a namespace - and that this document is not "namespace qualified" at document level.
I then went back to the XSD and defined a namespace for it by adding a "TargetNamespace" attribute to the root node as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://samplenamespace">
<xs:element name="RootNode">
I then used XML spy to generate a new sample XML instance document from this, updated XSD - this is what it came up with:
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2005 rel. 3 U (http://www.altova.com)-->
<RootNode xmlns="http://samplenamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://samplenamespace
A couple of things have changed:
  1. The attribute xmlns has been added to the root node and assigned the value "http://samplenamespace". This specifies that, unless explicitly qualified with a different namespace, all elements and attributes defined with the XML instance belong to the namespace "http://samplenamespace"
  2. The attribute schemaLocation (which happens to belong to the xsi schema instance namespace) has been added to the root node. The attribute contains a pair of values, the name of the namespace defined by the schema followed by a space then a URL reference to the XSD that defines the namespace
The above just skims the surface of what can be done with namespaces but in does provide the basics. There are many cases where an XML document will contain items defined in multiple namespaces. However, understanding such a document is straight-forward once you are aware of the key concepts and implementation as described above.

Update: 4th April 2012, had some more fun with XSDs today. Must remember, adding xmlns="x" to an XSD sets the namespace for that XSD; setting targetNamespace for an XSD gives the namespace for which other XSDs and XML can reference with an Import.

While creating the new schemas I came across a problem when trying to validate what seemed to be valid XML. Under the root element was <Invoice> , on attempting a validate from within XML Spy I received the message: "Unexpected element 'Invoice' in element 'InvoiceBatch'. Expected: Invoice". It turned out that this was because I had removed "elementFormDefault='qualified'", once I added this back in then it validated OK. 



Print | posted on Tuesday, November 24, 2009 4:43 PM


# re: XML Namespaces

left by Tom Koenig at 2/27/2010 6:25 PM Gravatar
Thanks. This really helped me sort some of this boiler plate xml header stuff.

# re: XML Namespaces

left by XML EDITOR at 3/29/2011 7:44 AM Gravatar
great post, like yourself I have come to rely on dedicated xml editors to shorten the learning curve and speed up actual coding work and would be lost without them.
Post A Comment