xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?
Asked Answered
S

2

94

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?

Shwa answered 10/12, 2015 at 13:18 Comment(0)
P
109

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.

Paraguay answered 10/12, 2015 at 14:44 Comment(8)
Thanks. So a single xmlns is used to define the default namespace for the document. Any element in the document without explicit namespace prefix will be in the default namespace. xmlns:xxx will define a non-default namespace, which must be explicitly prefixed to an element when using. Right?Shwa
You got it! Note also that a default namespace is not mandatory; you could instead declare a namespace prefix for http://maven.apache.org/POM/4.0.0 and use it explicitly.Paraguay
Thanks. I understand it now.Shwa
Lots more up-votes needed, this is by far the clearest explanation of this that I have found to date.Animalize
I'm still confused about the xsi namespace. In the example given, we declare the xsi namespace (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"), but do not give the xml processor its schema location. Could the xsi namespace be omitted completely? What would happen if no xsi namespace was declared, and the schemaLocation attribute was given without the xsi prefix?Reprisal
@jrahhali: No. You're missing two fundamental points about XML namespaces: (1) All used XML namespaces must be declared; (2) Namespaced component names are, by definition, fundamentally different component names than non-namespaced component names. If you have further uncertainties, please ask a detailed new question as a question, not as a comment. Thank you.Paraguay
So .xsd's targetNamespace should match its .xmls' xsi or default namespace?Archducal
@MinhNghĩa: targetNamespace should match the default or the explicit namespace of the root element of the governed XML document. Don't be confused by the word instance in xsi: xsi does not specify the XML namespace of an XML document instance; think of it more as an administrative mechanism in XML document instances as described above in this answer's bullet points for xsi:type, xsi:nil, xsi:schemaLocation, and xsi:noNamespaceSchemaLocation.Paraguay
H
27

xmlns defines default namespace, which states, that all nodes within project node and without an namespace-alias will be in http://maven.apache.org/POM/4.0.0 namespace by default.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defines namespace - http://www.w3.org/2001/XMLSchema-instance, and gives it a new alias - xsi

xsi:schemaLocation is an attribute schemaLocation of "http://www.w3.org/2001/XMLSchema-instance" namespace. It contains pairs of values - namespace URI and schema location link for xsd-schema file of that namespace. It can contain many pairs of values - one xsd file for every defined namespace URI. That means link http://maven.apache.org/xsd/maven-4.0.0.xsd contains xsd schema with definition of http://maven.apache.org/POM/4.0.0 namespace.

Healion answered 10/12, 2015 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.