Title: XSD: meaning of "URL attribute" of xs:schema element
Tags: xml;url;xsd;namespaces
Question: I think I understand what is the main purpose of namespaces in XML, however I do not understand why there are so many attributes. W3C school presents the following example:

```<?xml version="1.0"?&gt; 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified"&gt;
...
...
</xs:schema&gt;  
```

What is the difference between xmlns:xs, targetNamespace and xmlns ? Why should we specify three different URLs if none of them is "useful" to the schema? And in what this URL should be different?


Here is the accepted answer: xmlns:xs="http://www.w3.org/2001/XMLSchema"

This declares a namespace alias 'xs' for the namespace "http://www.w3.org/2001/XMLSchema". This is the namespace that defines the structure of an XML schema (XSD). All XML Schemas must be in this namespace.

xs:schema

The root element of the document, we can tell this is an XML schema document as the xs alias refers to the namespace "http://www.w3.org/2001/XMLSchema".

targetNamespace="http://www.w3schools.com"

This is the target namespace of the schema. This is the namespace that all the elements in this schema will be a part of. When you create an XML document that complies to this schema then the elements must be qualified with the namespace "http://www.w3schools.com". 

This can be omitted from the schema in which case all the element exist in an empty namespace. This is bad practice as when you get given an XML document like this, its difficult to tell what kind of XML document you are looking at (you can imagine a lot of companies creating schemas that describe an Invoice, all of which are specific to the company that created them).

xmlns="http://www.w3schools.com"

This sets the default namespace, it basically says that any items that you find from now on (that are not qualified with a namespace alias i.e. xs:element) are considered to be in this namespace. The reason for adding this is, it makes it possible to reference items within your schema, say you declare a , because you have a targetnamespace set, the qualified name for this type is AddressType@http://www.w3schools.com, you can only use it like this  because the value address type is is resolved using the default namespace (http://www.w3schools.com). You may often see the namespace used for the targetnamespace being aliased like this xmlns:ns="http://www.w3schools.com" instead. In which case when you would see this in the schema  where the AddressType is explicitly qualified.

elementFormDefault="qualified"

This is more complicated, and can largely be ignored. It is set on almost every schemas you will come across (its good practice to set it on any you create). 

So whats it do? Put simply it controls how you qualify namespaces in the output XML document. If its set to qualified then all elements must be be qualified in the XML document.

```<ns:root xmlns:ns="http://www.w3schools.com"&gt;
    <ns:other/&gt;
</ns:root&gt;
```

If its set to unqualified (or omitted - in which case it defaults to unqualified) then you don't need to qualify the child items in the XML document, its assumed because the parent is in a given namespace its children are to (note other has no namespace alias).

```<ns:root xmlns:ns="http://www.w3schools.com"&gt;
    <other/&gt;
</ns:root&gt;
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:12/20/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xml><url><xsd><namespaces>', 'Title': 'XSD: meaning of "URL attribute" of xs:schema element', 'ViewCount': 3500, 'id': 41241271, 'user_AboutMe': None, 'user_AccountId': 1382999.0, 'user_DisplayName': 'user1315621', 'user_Reputation': 2688.0, 'user_Views': 386.0, 'user_id': 1315621.0}}