XML Schema Definition:
A Definitive Quick Guide

The XML Schema Definition is a dense document that will take you a lot of time to parse to get to the heart of what you’re after. Even if you’ve worked with XML and XSDs for years, you may still need to drill down into it in order to resolve issues behind XML validation. I did this on a recent project where I had to research issue with XSD headers. I found some great resources that I want to share with you.

Deep Dive

XML Document

The XML document is often an instance of an XSD document (which is discussed below) this is done by declaring namespaces in the XML nodes as attributes.


Namespaces are defined in an XML document using the attribute xmlns.

  • A namespace defines semantics for a collection of tags and attributes.
  • Namespaces are only a case sensitive string that is usually a URI.

Every tag is comprised of a namespace and local-tag name as seen above. The namespace can be declared in a node, this is why we often only see the local-tag portion.

For example:

<root xmlns="">
    <test />

This is indicating that the default namespace for the document is ‘’

We can also prefix the namespace. This is like creating a shortened reference to the namespace.

For example:

<foo:root xmlns:foo="">

Notice the difference between the two examples above: xmlns:foo and xmlns.

So why wouldn’t just use the default? Well, we can import multiple sets of XML sematics and mix them into a document. In this case we want to be able to tell the reader how to treat each node.

For example:

<foo:root xmlns:foo="" xmlns:bar="">
    <foo:test />
    <bar:test />

Lastly the schemaLocation attribute points to where the defined schemas can be found. The schemaLocation can be a bit more tricky

  • If one location is defined, then it points to the schema location for the document
  • It can also be defined as multiple pairs separated by a space, such as where the first half of the pair is the namespace and the second half of the pair is where to find the document to be validated. This gives the user power to defined multiple pairs if multiple schema namespaces are imported.
  • The schema locations can be a web address or a local location.
  • The schemaLocation attribute is defined by W3C’s schema instance namespace, which is declare as
<root xmlns:xsi="">

XML Schema Definition

The XML Schema Definition (XSD) defines the protocol for XML document instances. The scope of this article is to discuss header declarations and not the specifics of how to define an XSD.

The XSD is actually instance of an XSD defined by W3C. So when defining a schema we want to declare the W3C schema namespace. This is done using Note: this is imported to a namespace xmlns and assigned to the prefix xs. Just like the examples above we can assign the namespace to any prefix or no prefix allow it to be the default namespace.

<xs:schema xmlns:xs="">

The targetNamespace attribute is used to define what namespace the XSD document is defining. In other words, if your XML document declares xmlns:foo=”” The corresponding XSD document should have a targetNamespace attribute of targetNamespace=””

<xs:schema xmlns:xs="" targetNamespace="">...</xs:schema>


How schemas and schema instances are defined can seem quite confusing, but understanding a few basic concepts helps clarify and show how simple using them actually is. The best way to learn is to read this blog and play with some of the concepts using an XML schema validation. A link to a simple Java package that will validate XML to XSDs is located in the resources below.

  • xmlns — is used to declare the a namespace in the document. Without a prefix it is used as the default namespace of the document.
  • xmlns:prefix — declares a namespace and assign the namespace to a prefix. Meaning if the prefix is used before a node tag it is assumed that the node is defined by that prefix (e.g. <xs:schema>).
  • xmlns:xs= — Used in XSDs to declare that the document is specifying a schema. Note: xs or xsd is often used by convention for this declaration.
  • xmlns:xsi: — Declared in the XML and is used to help declare that the XML document is an instance of and XSD. The convention is to use the namespace prefix of xsi.
  • targetNamespace: is used in the header of the XSD to defined the namespace that the XSD describes.
  • schemaLocation – Gives the location of one or more XSDs used in the XML document.


Categories: Blog

Tags: ,

Ryan Van Fleet
06 Jun, 2018