Alex Rivera | Logout

XML Schema Header & Namespace Config

Asked 2011-11-22T15:58:23.617
18

Migrating from DTD to XSD and for some reason the transition is a bumpy one. I understand how to define the schema once I'm inside the <xs:schema> root tag, but getting past the header & namespace declaration stuff is proving to be especially confusing for me.

I have been trying to follow the well-laid out tutorial on W3S but even that tutorial seems to assume a lot of knowledge up front.

I guess what I'm looking for is a King's English explanation of which attributes do what, where they go, and why:

  • xmlns
  • xmlns:xs
  • xmlns:xsi
  • targetNamespace
  • xsi:schemaLocation

And in some cases I see different variations of these elements/attributes, such as xsi which seems to have two different notations like xsi:schemaLocation="..." and xs:import schemaLocation="...".

I guess between all these slight variations I can't seem to make heads or tails of what each of these does. Thanks in advance for bringing any clarity to this confusion!

Edit
Report

2 Answers

67

The first thing you'll need to understand are XML namespaces. If you have some time to waste, you could read the specification. I found this to be one of the clearer specs related to XML. It doesn't matter if you don't understand everything it says, it's a good basis. But here's a quick rundown.

XML elements and attributes have a name. When you see <test att="hello"/>, you're looking at an element with name "test", in which we have an attribute with name "att". But this isn't really the entire story...

XML is a syntax that allows you to mix content from different markup languages. For example, when using XSLT to turn an XML document into an XHTML page, you're dealing with at least three markup languages defined in XML: your input document's, XSLT and XHTML. Such mixes would become rather hard if each one reserved its own element/attribute names and no collisions were ever allowed.

Enter XML namespaces. An XML namespace defines a "sphere" within which element and attribute names have actual semantics. The element "template" has a well-defined meaning in the XSLT namespace. The element "complexType" has a well-defined meaning in the XML Schema namespace. If you wish to use either in your own markup language using XML, then that's possible provided you do so in a different namespace.

In order to make sure a namespace is unique, you'll need to provide some unique identifier. The specification settled on the use of URIs, most often in the form of a HTTP URL. The reason for this is simple: such URLs tend to be good unique identifiers. But it's also a very common cause for confusion because people think the URLs really hold meaning or will be accessed over the network during XML processing. Know very well that this is not the case! The URL is not required to point to any existing resource. It will not go through any transformation or be

answered 2011-11-22T17:46:21.227
2

In answer to your queries:

  1. xmlns - a unique value used in instance documents to indicate which schema the XML is an instance of (or what schema it will validate against). Even though the namespace does not identify the actual schema file, there should be one schema which defines this namespace.
  2. xmlns:xs - called namespace prefixes and by convention used in instance documents to indicate where the types used in the XML are coming from. You can think of this like using in C# or imports in VB. For example, xmlns:xs="http://mySharedTypes" says that in this XML some of my types come from the namespace "http://mySharedTypes", and these types will be prefixed with "xs".
  3. xmlns:xsi - as above. Infact the xs and xsi prefixes are by convention used when referencing the W3C schema namespaces http://www.w3.org/2001/XMLSchema and http://www.w3.org/2001/XMLSchema-instance. However these namespace prefixes are just convention and can actually be anything.
  4. targetNamespace - a unique value you put in your schema definition which gives the types you define in the schema their namespace. So if someone wants to use the types in your schema then they must include an xmlns attribute with the same values as your targetNamespace.
  5. xs:import schemaLocation - This is conventionally a relative path to another schema, although not all xml processors recognise it. So you can optionally link to another schema in your xs:import as a kind of shortcut to the schema file itself. The other attribute in the import is the schema targetNamespace, which is mandatory.
  6. xsi:schema
answered 2011-11-22T16:31:24.993

Your Answer