Search code examples
xmlxsdw3c

Some questions about name spaces on xml?


  1. Is a namespace just a tag, which says schema-validator that this element behaves "so" and will behave "same" as long as it is tagged with this namespace?

  2. What are default namespaces?

  3. In a case like that belove,

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">

Does xmlns:xs mean http://www.w3.org/2001/XMLSchema namespace will be presented with xs and elements, which are not tagged with xs, are coming from the namespace http://www.w3schools.com?


Solution

  • Is a namespace just a tag, which says schema-validator that this element behaves "so" and will behave "same" as long as it is tagged with this namespace?

    No, an XML namespace is not a tag at all. It is much like a namespace in any programming language: a largely abstract context within which certain names exist and are unique. Inasmuch a given name may exist and have different meanings in different namespaces, correct interpretation of any unqualified name depends on knowing or guessing its correct namespace (including if it has no namespace).

    XML Namespaces are identified by characteristic URIs, but it's best to think of those only as concrete handles, not as the namespaces themselves. This is akin to the distinction between your name and you, yourself. Distinct namespace URIs identify distinct namespaces.

    There are tons of analogies, both in and out of technical arenas. For example, in the United States, one can pretty much rely on no two cities in the same state having the same name, but different cities in different states can and do have the same name. The states serve, in small part, as namespaces for city names. Thus, if I ask you to go to Springfield, you may be able to guess whether I mean the one in Missouri, the one in Illinois, the one in Massachusetts, or one of the others, but unless I somehow tell you which one, you can't be sure.

    What are default namespaces?

    It's tedious and a bit ugly to express all XML elements with qualified names. An XML document can instead declare a default namespace, in which case unqualified element names are to be interpreted relative to that namespace, instead of being interpreted as having no namespace. (Attribute names are not directly impacted by the default namespace.)

    It's like if we were part of a small, Illinois-based company, such that by default, we always interpreted city names as Illinois city names. Then if I asked you to go to Springfield without saying which one, you would be justified in interpreting me to mean Springfield, Illinois. If I wanted you to go to Springfield, Missouri instead then I would be obligated to say so specifically.

    Does xmlns:xs mean [...]?

    All the above is somewhat theoretical. What you're asking about now is basically how those theoretical concepts are expressed in XML.

    XML uses namespace prefixes to associate local names with namespaces. The result is a qualified name of the form prefix:local. For example, the start tag presented in the question is for an element with qualified name xs:schema; this element has local name "schema" and namespace prefix "xs". It's a bit like the U.S. Post Office assigning "MO" as an abbreviation of "Missouri", so that we can address mail to "Springfield, MO" and they know what we mean.

    Of course, that begs the question of how namespace prefixes are associated with specific namespaces.

    First, there are two pre-defined namespace prefixes, "xml" and "xmlns", which are always bound to the namespaces identified by http://www.w3.org/XML/1998/namespace and by http://www.w3.org/2000/xmlns/, respectively. Other prefixes must not be bound to those namespaces, those namespaces may not be assigned as the default namespace, and those bindings must not be removed, therefore XML processors can recognize names in those namespaces by prefixes alone, without performing any other namespace processing.

    Second, attribute names with namespace prefix "xmlns" are defined to describe the association between namespace prefixes and namespaces for the element on which they appear and its descendants (subject to override within those descendants). The local part of such an attribute name is the namespace prefix being bound, and the value of the attribute as the namespace URI for the namespace associated with the prefix.

    XML also provides for default namespaces. The attribute name xmlns is defined to declare the default namespace for the element on which it appears and its descendants (subject to override within those descendants). The value of the attribute is the namespace URI of the default namespace. Where such a default namespace declaration is in scope, unqualified element names are interpreted as belonging to the default namespace.

    Thus, taking your example start tag:

    <xs:schema
    

    The element has local name "schema" and is associated with a yet-to-be-specified namespace bound to prefix "xs".

    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    

    The namespace prefix "xs" is bound to the namespace identified by URI http://www.w3.org/2001/XMLSchema in this element and its descendants, including in this element's own name.

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

    The attribute targetNamespace of the element is declared to have value "http://www.w3schools.com". The significance of this is specific to the element type (in this case it declares the namespace URI of the elements and attributes defined by the schema represented by that element).

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

    The default namespace for the element and its descendants is the one identified by URI http://www.w3schools.com.

    elementFormDefault="qualified"
    

    Another attribute specific to the element type.

    >