Abbey Workshop

Ant: Cannot find the declaration of element

I ran across an odd problem today. While trying to get Apache Ant 1.6.1 to perform schema validation on all the XML files in a directory, the following error is generated:

		
validate_project_files:
[xmlvalidate] /user/dir/path/project1.prj.xml:6:2: cvc-elt.1: 
Cannot find the declaration of element 'project'.

Here is the Ant script that would not run:

Listing for: build_bad.xml

   1:<project name="dashboard" default="generate_dashboard">
   2:
   3:    <target name="validate_project_files">
   4:        <!-- Make sure the files are valid before generating dashboard -->
   5:        <xmlvalidate    failonerror="yes" lenient="no" warn="yes"
   6:        >
   7:            <fileset dir="." includes="*.prj.xml"/>
   8:            <attribute name="http://xml.org/sax/features/validation" value="true"/>
   9:            <attribute name="http://apache.org/xml/features/validation/schema"  value="true"/>
  10:        </xmlvalidate>
  11:        <echo message="Project files have been validated"  />
  12:    </target>
  13:
  14:    <target name="generate_dashboard" depends="validate_project_files">
  15:        <!-- Generate a dashboard from the project files -->
  16:        <xslt in="project_list.xml" out="dashboard.html"
  17:            style="project.xsl" force="true"
  18:        />
  19:    </target>
  20:</project>

Using Ant to do mass XML schema validation seems pretty straightforward. You create a target that contains the xmlvalidate task, create a fileset like line 7, and turn on schema validation with lines 8 and 9. (XML Schema validation is not enabled by default in the Xerces parser, which is included with Ant.) That's it, it should work, right? Well it didn't. I tried everything I could. Changed the schema link from a relative to an absolute path. Tried with and without a namespace. No change.

So taking a look at some other examples, there seemed to be a couple more options that could be added to the xmlvalidate task. So to make a long story short, adding lines 6 and 7 to the file solved the problem.

Listing for: build.xml

   1:<project name="dashboard" default="generate_dashboard">
   2:
   3:    <target name="validate_project_files">
   4:        <!-- Make sure the files are valid before generating dashboard -->
   5:        <xmlvalidate    failonerror="yes" lenient="no" warn="yes"
   6:                        classname="org.apache.xerces.parsers.SAXParser"
   7:                        classpath="lib/xerces.jar"
   8:        >
   9:            <fileset dir="." includes="*.prj.xml"/>
  10:            <attribute name="http://xml.org/sax/features/validation" value="true"/>
  11:            <attribute name="http://apache.org/xml/features/validation/schema"  value="true"/>
  12:        </xmlvalidate>
  13:        <echo message="Project files have been validated"  />
  14:    </target>
  15:
  16:    <target name="generate_dashboard" depends="validate_project_files">
  17:        <!-- Generate a dashboard from the project files -->
  18:        <xslt in="project_list.xml" out="dashboard.html"
  19:            style="project.xsl" force="true"
  20:        />
  21:    </target>
  22:</project>

Upon further review, line 6 seemed to be key. I'm guessing this is some sort of classpath problem (more than one Xerces.jar file in my classpath).