Saturday, 15 September 2012

Explain: xmlns, xsd, xsi:schemaLocation

This post is a reminder/introduction to some XML related terminology and acronyms.

xmlns

xmlns stands for XML namespace. This item is used to declare namespaces in XML documents. It allows one use the XML element and attributes of this namespace in the XML document.

Several namespaces can be declared in a single XML document. In order to avoid name collisions, one can add a prefix (i.e., xmlns:myprefix) to differentiate between same XML elements and attributes.
For example:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
  xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans"
  ... >
    ...
</beans:beans>
The above declares two namespaces, a default one without prefix (security) and one with the beans prefix (beans). One can use all items of the security namespace without prefix, but the elements and the attributes of the bean namespace must be used with the beans prefix, such as <beans:beans ...> and </beans:beans>.

xsd

An xsd file defines a XML schema, that is, how elements and attributes of an XML document can interact with each other. Typically, these xsd files are used to validate XML documents.

xsi:schemaLocation

Once one includes the XML schema instance namespace in a document (typically with xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"), one can specify the XML schema's location against which the XML document should be validated, with xsi:schemaLocation:
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.1.xsd"