Aseem Bansal
Aseem Bansal

Reputation: 6972

Why is there redundancy in xsi:schemaLocation declaration?

The xml schema location contains http://www.springframework.org/schema/beans which is already the schema global namespace. Then why is it always repeated in the xsi:schemaLocation element? The XSD/DTD defines the XML and so is needed for XML validation by the parser but why the repeated namespace?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       <bean id="test" />
</beans>

Upvotes: 1

Views: 178

Answers (2)

Ian McLaird
Ian McLaird

Reputation: 5585

It only looks redundant because you only have one namespace schema defined. In cases where there are multiple schemas, you need to pair up the namespace with its schema definition.

<foo xmlns="http://www.example.com/foo"
     xmlns:bar="http://www.example.com/bar"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.example.com/foo http://www.example.com/foo.xsd
                         http://www.example.com/bar http://www.example.com/bar.xsd">
  <bar:tag />
</foo>

This means that the default namespace is http://www.example.com/foo with its schema located at http://www.example.com/foo.xsd, but we're also using the namespace http://www.example.com/bar whose schema is located at http://www.example.com/bar.xsd The schemaLocation attribute is a whitespace delimited list of namespace/schema pairs.

Upvotes: 2

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

This

xmlns="http://www.springframework.org/schema/beans"

states that the default namespace, ie. if none is specified, is http://www.springframework.org/schema/beans.

This

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

states that the namespace http://www.springframework.org/schema/beans can be validated with the XSD found here: http://www.springframework.org/schema/beans/spring-beans.xsd.

Upvotes: 2

Related Questions