Using XML namespaces and the importance of exactitude

I wan’t to share what I first thought was a bug, then a configuration error on my part and what actually happened.

Lately I’ve been trying to configure a Tomcat webserver with Spring framework, to play around with on my projects. Anyone who has done these things before know that there are some configuration that needs to be done in order for all things on a server to play nicely, such as declaring dependencies in pom.xml and various other configuration files needs to be written. These files are written in Extensible Markup Language or XML, which is a language where so called namespaces can be declared.

A namespace can be viewed as a collection of terms used in a certain context as key value pairs. Namespaces are great for structuring data and to remove any ambiguity existing between terms used in a document. For example:

xmlns:blog="path.to.blog"
xmlns:post="path.to.post"
<blog:name>Christoffers blog</blog:name>
<post:name>Using XML namespaces and the importance of exactitude</post:name>

Above XML uses two different namespaces with the same attribute name. By using namespaces we can distinguish the attribute name by deriving it from a specific namespace standard and thus no ambiguity on what name we are referring to exists.

The issue I ran into were not understanding the error message given to me by Eclipse when trying to fetch the Spring beans namespace. The error stated:

cvc-elt 1 cannot find the declaration of element 'beans'

So I checked my imported namespaces in the xsi:schemaLocation (which is used to fetch specified namespaces from the internet)

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

According to my logic based on the error message there seemed to be something going wrong importing the spring-beans namespace. I checked the internet for this problem, and came across solutions regarding changing the spring-beans.xsd to the current version of Spring that I’m using. Tried that and it still didn’t solve the issue, and I also tried some other suggestions such as putting all namespaces on the same line (ugh ugly!) which also failed. After about two hours of failing to find a solution, (and the suggestions where getting even more bizarre as time went on) I found the issue.

This is where the importance of exactitude comes in to play. When using the schemaLocation from the xsi, it is generally good practice if the namespace itself is downloaded in prior statement. This is what my full statement looked like:

<?xmlversion="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 <import resource="datasource-config.xml"/>
 <import resource="webflow-config.xml"/>
 <!-- <import resource="faces-config.xml"/> -->
 
 </beans>   

Now I had focused all my attention on the spring-beans import considering the error statement. Now the problem was in the line
xmlns:xsi="http://www.w3.org/2001/XMLSchema

This line is responsible for fetching and declaring the xsi namespace for further use in the document. Below is how the statement should have looked
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

The extra -instance on the end of the statement was needed for the xsi namespace to correctly resolve and to be used in order to fetch other namespaces such as the needed spring-beans namespace. I found this out by comparing a working config file which according to me did nothing different. This became a lesson in being thorough and sure of what we are declaring when configuring a project from the start, because things like this can take time to debug because of ambiguous error messages which are hard to interpret.

Hope you enjoyed the read 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *