3

I understand that, by convention, the default namespace is named xmlns attribute in the root element of an XML document. Other namespaces are named other names. Now, I wonder why is XML Scheme Instance namespace always named xmlns:xsi and not simply xsi? Would xsi be the same as xmlns:xsi? If yes, why yes? If no, why no?

Elements and attribute names not coming from the default namespace are always preceded by name of the namespace. Having xmlns:xsi tells me that xsi is an attribute defined in namespace xmlns, which is conflicting, since the attribute names from the default namespace shouldn't be preceded by the namespace.

gicig
  • 334
  • 3
  • 14

1 Answers1

3

The default namespace is not named xmlns.

xmlns is the way to declare a namespace prefix (i.e. short name, handle). The syntax is xmlns[:prefix]="namepace-uri".

There can be exactly one namespace declaration per XML element where you are allowed to leave off the prefix (xmlns="namespace-uri"), and if it is declared that way, it is called the default namespace.

It's called default because all descendant elements that don't explicitly override it will inherit it - here <element>, <child> and the first <grandchild> are all part of some_namespace:

<element xmlns="some_namespace">
  <child>
    <grandchild />
    <grandchild xmlns="something_else" />
    <yan:grandchild xmlns:yan="yet_another_namespace" />
  </child>
</element>

This automatic inheritance does not happen with prefixes - here, only <sn:element> is in some_namespace, and therefore identical to the <element> above, while <child> and the first <grandchild> are in no namespace:

<sn:element xmlns:sn="some_namespace">
  <child>
    <grandchild />
    <grandchild xmlns="something_else" />
    <yan:grandchild xmlns:yan="yet_another_namespace" />
  </child>
</sn:element>

The important part is only the namespace URI, not the prefix:

  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declares that the URI http://www.w3.org/2001/XMLSchema-instance shall be known as xsi inside this element.

  • You are free to choose any prefix you like, you can declare xmlns:bob="http://www.w3.org/2001/XMLSchema-instance" and that would mean that the URI http://www.w3.org/2001/XMLSchema-instance shall be known as bob inside this element.

By convention, many widely-used XML namespace URIs get the same prefix everywhere, but that's not technically necessary.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • "`xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` declares that the URI 'http://www.w3.org/2001/XMLSchema-instance' shall be known as `xsi` inside this element.". Does it mean that `xsi` is now a namespace whose elements now can be used within the document? If yes, what is the scope? The element in which it was defined or the whole document? – gicig Aug 14 '16 at 10:18
  • 2
    `xsi` is not a namespace. `http://www.w3.org/2001/XMLSchema-instance` is the namespace. `xsi` is merely the short name for that namespace, commonly called "prefix" because you write it as a prefix before elements you want to be in the associated namespace. The scope of a namespace declaration is the element that it appears on and everything inside. – Tomalak Aug 14 '16 at 10:22
  • Yes, that is what I meant, but didn't express myself properly. I meant the namespace represented by the name, i.e., by `xsi`. Thank you very much for the clarification. – gicig Aug 14 '16 at 10:29
  • Readers falling in this question may be looking for the actual purpose of xsi / xsd prefixes, which by convention, refer to XML Schema Document and XML Schema Instance, and the answer is here https://stackoverflow.com/questions/41035128/what-is-the-difference-between-xsd-and-xsi – Jose Manuel Gomez Alvarez Nov 05 '21 at 12:53