Monday, April 13, 2015

How to resolve "A schema cannot contain two global components with the same name"?

When I have a project using spring-data-cassandra, the XML configuration applicationContext.xml's header looks like below.


...

Eclipse always complains the following errors:

Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans.xsd).  For more information, right click on the message in the Problems View and select "Show Details..."
Referenced file contains errors (http://www.springframework.org/schema/tool/spring-tool.xsd).  For more information, right click on the message in the Problems View and select "Show Details..."

The details are shown as below.

The errors below were detected when validating the file "spring-beans.xsd" via the file "applicationContext.xml".  In most cases these errors can be detected by validating "spring-beans.xsd" directly.  However it is possible that errors will only occur when spring-beans.xsd is validated in the context of applicationContext.xml.

sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframwork.org/schema/beans,identifiedType'. line 43
...

The problem is caused, I believe, by the version in different XSDs. For example, spring-context.xsd imports spring-beans.xsd and spring-tools.xsd, and always uses the links with a version like this: http://www.springframework.org/schema/beans/spring-beans-4.1.xsd.




...

But spring-cassandra.xsd imports XSDs without version.




 
...

If you download the following xsd, spring-beans, spring-tool, spring-cassandra, spring-context and spring-repository, and change all schemaLocations to point a local file like "file:///tmp/spring-beans.xsd", the validation errors will be gone.

This is really annoying when you see red X. Here are my suggestions:

  • remove xsi:schemaLocation in your applicationContext.xml, there will be a warning like "No grammar"
  • Window->Preferences->XML->XMl Files->Validation->uncheck "Honor all XML schema locations"