xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

smwikipedia picture smwikipedia · Dec 10, 2015 · Viewed 41.9k times · Source

For the following XML fragment:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

What do the xmlns, xmlns:xsi, and xsi:schemaLocation attributes exactly mean? How are they related? What's the : for?

And there are 2 URLs in the xsi:schemaLocation=

  1. http://maven.apache.org/POM/4.0.0 (it happens to be the same as xmlns but it gives a 404 error when visiting.)
  2. http://maven.apache.org/xsd/maven-4.0.0.xsd (this is an actual XSD doc)

If 1 doesn't exist, why still put it there?

Answer

kjhughes picture kjhughes · Dec 10, 2015

Namespace related attributes in XML and XML Schema (XSD)

  • xmlns is part of the W3C Namespaces in XML Recommendation:

    The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/.

    In your example, it declares that http://maven.apache.org/POM/4.0.0 is the default namespace for the elements in your Maven project.

  • xmlns:xsi declares a standard namespace prefix (xsi) for a core namespace used in XSD: http://www.w3.org/2001/XMLSchema-instance

    XML Schema: Structures also defines several attributes for direct use in any XML documents. These attributes are in a different namespace, which has the namespace name http://www.w3.org/2001/XMLSchema-instance. For brevity, the text and examples in this specification use the prefix xsi: to stand for this latter namespace; in practice, any prefix can be used.

    In your example, it declares the conventional binding of the xsi namespace prefix to http://www.w3.org/2001/XMLSchema-instance, which properly sets up the use of the following attributes:

    • xsi:type allows an XML instance to associate element type information directly rather than through an XSD. See How to restrict the value of an XML element using xsi:type in XSD?

      In your example, xsi:type is not used; included here for completeness regarding xsi.

    • xsi:nil allows an empty element to be considered to be valid when the XSD might not otherwise have allowed it.

      In your example, xsi:nil is not used; included here for completeness regarding xsi.

    • xsi:schemaLocation and xsi:noNamespaceSchemaLocation provide hints to the XML processor as to how to associate an XSD with an XML document. Use xsi:schemaLocation when there is a namespace; use xsi:noNamespaceSchemaLocation when there is no namespace.

      In your example, there is a namespace, so you properly use xsi:schemaLocation, whose values are space-separated pairs of namespace and XSD-location-URI. Your example uses the namespace, http://maven.apache.org/POM/4.0.0, and namespaces are lexical naming constructs that need not be retrivable. Your example also uses the XSD-location-URI, http://maven.apache.org/xsd/maven-4.0.0.xsd, which is retrivable as it should be.

      If your example did not use a namespace, you would use xsi:noNamespaceSchemaLocation, whose value is a single XSD-location-URI that hints to the location of the intended XSD and which should be retrievable.

  • targetNamespace is an attribute on the xs:schema root element of an XSD which specifies the namespace of the root element of the XML document instances the XSD is intended to govern. It must match the default or explicit namespace of those XML documents' root elements.