-->

org.xml.sax.SAXParseException: cvc-complex-type.2.

2020-07-23 06:07发布

问题:

org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'drools:grid-node'. 

I'm getting this error when I add a grid-node and ksession to my spring xml. I did some searching and looks like it a classpath issue. What dependency am I missing here ?

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:drools="http://drools.org/schema/drools-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
       http://drools.org/schema/drools-spring org/drools/container/spring
       http://drools.org/schema/drools-spring org/drools/container/spring/drools-spring-1.2.0.xsd">


    <drools:grid-node id="node1"/>
    <drools:ksession id="ksession1" type="stateful" kbase="kbase1" node="node1" />

My pom.xml has the following for Drools.

               <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-camel</artifactId>
        <version>${drools.version}</version>
        <exclusions>
            <!-- This ensures that we use the latest version of Spring jars and not 
                the one that comes with drools.version. -->
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>

            <exclusion>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-core</artifactId>
            </exclusion>

            <exclusion>
              <groupId>org.apache.camel</groupId>
              <artifactId>camel-spring</artifactId>
            </exclusion>



            <exclusion>
                <artifactId>camel-xstream</artifactId>
                <groupId>org.apache.camel</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>knowledge-api</artifactId>
        <version>${drools.version}</version>
    </dependency>

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${drools.version}</version>
    </dependency>


    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>${drools.version}</version>
    </dependency>


    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-spring</artifactId>
        <version>${drools.version}</version>
    </dependency>

回答1:

This error is being issued because the Drools XSD can't be located. In this comment in the Drools user list, it is stated that the XSD's aren't publicly available, and the xsi:schemaLocation attribute in the XML is mapping that XSD to http://drools.org/schema/drools-spring org/drools/container/spring/drools-spring-1.2.0.xsd, which doesn't resolve to the proper XSD.

Spring should be automatically handling the XSD resolution given the xmlns:drools="http://drools.org/schema/drools-spring" attribute. One of the Drools JARs should be including a META-INF/spring.handlers file defining the XSD mapping for the drools namespace. Something along the lines of :

http://drools.org/schema/drools-spring=some.classpath.visible.package.xsdfile.xsd

Which should be automatically handling the XSD file included in the Drools JARs.

Try removing the last two lines of your xsi:schemaLocation attribute in order to let Spring automatically resolve the XSD.

Some related links:

  • Spring schemaLocation fails when there is no internet connection. Specially David Resnick's answer.
  • Spring reference's Appendix D.5 Registering the handler and the schema.

Of course, you could also extract that XSD from the JAR file, place it in an accessible directory from your classpath and use a classpath relative URL in xsi:schemaLocation.

By the way, it's probably a copy&paste error, but your <beans> element is missing its closing tag.

EDIT : It seems that Drools wasn't providing the spring.handlers properly (at least as of December 2010, see Drools + Spring without internet ). You might need to dig through the JARs to get the XSD and reference it directly in xsi:schemaLocation.



回答2:

As for the error marker in Eclipse: I have only a workaround, and I doubt that there is a real solution for this. As Xavi Lopez stated in his accepted answer, there is a spring-handlers file in the drools-spring jar (for me, drools-spring-5.3.0.Final.jar), under META-INF. This, however, does not reference an xsd file (although the xsd files reside in this very jar), but contains this:

http\://drools.org/schema/drools-spring=org.drools.container.spring.namespace.SpringDroolsHandler
http\://drools.org/schema/drools-spring-1.2.0=org.drools.container.spring.namespace.SpringDroolsHandler
http\://drools.org/schema/drools-spring-1.3.0=org.drools.container.spring.namespace.SpringDroolsHandler
http\://drools.org/schema/drools-spring-1.4.0=org.drools.container.spring.namespace.SpringDroolsHandler
http\://drools.org/schema/drools-spring-1.5.0=org.drools.container.spring.namespace.SpringDroolsHandler

As you can see, it defines a handler class for each version of the xsd. This works well with spring at runtime, but I never saw it working with any version of Eclipse (nor the Spring Tool Suite).

I always end up turning off validation for xsd files in the project, which - for your convenience - goes like this:

  • Select your project in Package Explorer, click File/Properties (or press Alt+Enter)
  • In the Properties for project dialog, select Validation
  • Check Enable project specific settings (disable the validation only for this project)
  • Uncheck Manual and Build for: XML Schema Validator
  • Press OK
  • Right-click on the project and select Validate (to revalidate the project with the modified settings)

(I also tried to reference the xsd directly from the spring beans definition xml, but it did not work. As the handler defined in the spring-handlers file works at runtime, this would not be a better workaround IMHO.)