Apache Ant/XMLvalidate

Motivation
You want a command-line interface to validate one or more XML files.

Instructors Note: This file is used as a lab exercise for an Apache Ant class that includes extensive use of XML.

Method
You can use Apache ant to check a file or group of files for their validity. This is done by using the   Apache Ant task. The xmlvalidate ant task will use a standard ant and go through and check each file. In the example below, we specify a directory called "in" using a property. We then use the fileset to find all XML files in that directory and all subdirectories of that directory. Each file is successfully checked for validity against an XML schema.

Sample Ant Task to Validate All XML Files in a Folder
      

In the above example, we assume that each XML file has a directive that tells it where to get its XML Schema.

This target will run the default XML parser that comes with Ant (usually Xerces) and report any file that is not well-formed.

Sample XML Schema MyMessages.xsd
To test this you will need a small XML Schema file. The following file read a files of three messages:

         </xs:element> </xs:schema>

Sample Valid Data File
Here is a sample message file:

<?xml version="1.0" encoding="UTF-8"?> <MyMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MyMessages.xsd"> <MyMessage>Hello World!</MyMessage> <MyMessage>ANT AND XML Schema ROCK</MyMessage> </MyMessages>

Note that the noNamespaceSchemaLocation attribute of the root element tells it to look in the current directory to find the XML schema file (MyMessages.xsd)

Sample Invalid Data File
If you add a fourth message the file should fail validation according to the rules in the XML Schema above.

<?xml version="1.0" encoding="UTF-8"?> <MyMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MyMessages.xsd"> <MyMessage>Hello From XSLT</MyMessage> <MyMessage>From input: Hi</MyMessage> <MyMessage>ANT AND XSLT ROCK</MyMessage> <MyMessage>I am the fourth message.</MyMessage> </MyMessages>

To test this example, add a folder called "in" and put several XML files in the folder that are not valid. In this case we created a invalid file called MyInputBad.xml. When we type "build" at the command line the following was the output:

Sample Output
ValidateXML: [xmlvalidate] C:\XMLClass\Ant\validate\in\MyInput.xml:6:15: cvc-complex type.2.4.d:     Invalid content was found starting with element 'MyMessage'. No child element is expected at this point.

This is a sample output. Note that the error message does not indicate that you exceed a count of 3 data elements.

Supplying an XML Schema definition file
If you are working in the null namespace add the following attribute:

If your documents have a namespace use the following:

If the XML files do not include their own schema, you can also create an ant task that includes where to find the XML schema. This is done using an special ant property.

<property name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" value="${xsd.file}"/> <xmlvalidate file="xml/endpiece-noSchema.xml" lenient="false" failonerror="true" warn="true"> <attribute name="http://apache.org/xml/features/validation/schema" value="true"/> <attribute name="http://xml.org/sax/features/namespaces" value="true"/>

Schematron Validate
Apache ant also has an element to validate against a schematron rules file

See http://www.schematron.com/resource/Using_Schematron_for_Ant.pdf for more details.

Navigation
Previous Chapter, Next Chapter