How to validate an XML document using a RELAX NG schema and JAXP? How to validate an XML document using a RELAX NG schema and JAXP? xml xml

How to validate an XML document using a RELAX NG schema and JAXP?


I resolved this very error on Java 1.6 with the following line:

// Specify you want a factory for RELAX NG "compact"System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory");SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);

This allows me to use Jing to validate an XML document against a Compact RELAX NG schema. Full example below. I didn't use the bridge or anything else. The runtime classpath only has jing.jar (20091111) and my own Validator class.

import java.io.File;import java.io.IOException;import javax.xml.XMLConstants;import javax.xml.transform.stream.StreamSource;import javax.xml.validation.Schema;import javax.xml.validation.SchemaFactory;import javax.xml.validation.Validator;import org.xml.sax.SAXException;public class Validate{    public static void main(String[] args) throws SAXException, IOException    {        // Specify you want a factory for RELAX NG        System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory");        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);        // Load the specific schema you want.        // Here I load it from a java.io.File, but we could also use a        // java.net.URL or a javax.xml.transform.Source        File schemaLocation = new File(args[0]);        // Compile the schema.        Schema schema = factory.newSchema(schemaLocation);        // Get a validator from the schema.        Validator validator = schema.newValidator();        for (int i = 1; i < args.length; i++)        {            String file = args[i];            // Check the document            try            {                validator.validate(new StreamSource(new File(file)));                System.out.println(file + " is valid.");            }            catch (SAXException ex)            {                System.out.print(file + " is not valid because: " + ex.getMessage());            }        }    }}

Once again, I've only tested this ion Java 1.6.

$ java -versionjava version "1.6.0_01"Java(TM) SE Runtime Environment (build 1.6.0_01-b06)Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)


See Stefan Bodewig's Weblog written on March 7, 2008 titled RELAX NG Validation in XMLUnit:

Since last night XMLUnit's trunk contains a new Validator class that is based on javax.xml.validation which is part of JAXP 1.3 (i.e. Java5+).

...

To the best of my knowledge there is no JAXP implementation that supported RELAX NG out of the box. Sun's own JAXP 1.4 (Java6+) certainly doesn't. Some searching around brought me to Kohsuke Kawaguchi's Blog who should know, given his work on JAXP, Sun's Multi Schema Validator, isorelax and other stuff.

Using his isorelax-bridge and Jing didn't get me anywhere on Java6. I went back to Kohsuke Kawaguchi's article and read the comments: the bridge doesn't work with Java6 since they changed the SchemaFactory lookup algorithm. OK, tried Java5 instead - progress, I now get a NullPointerException somewhere inside of Jing, so at least it is loading the factory. Next I replaced Jing with MSV (which is here now, no matter how many links out there lead you to the WebServices stack page at Sun, so much for "good URLs never change") and really, my simplistic tests pass.

So you may have to jump through some hoops to get RELAX NG support into your JAXP setup - in my case Java5, MSV and Kawaguchi's bridge worked, but the comments indicate it should be doable with Java6 as well - but once you manage to configure everything correctly, XMLUnit will now be there to let you assert your document's validity in Unit tests. It seems that it doesn't work for compact syntax, though.

To read the comments on Kohsuke Kawaguchi's blog, you have to use archive.org because somehow they are all gone now:

Java 5 interprets the Service Providerfile as a list of key/value pairs,which is a violation to the Java 5 & 6JAR file specification but happens tomatch your example.

Java 6 parses the Service Providerfile as specified, ie. as a list offully qualified class names, but thusfails to instantiate your adapter'sSchemaFactory as the Service Providerfile's contents are invalid.

To be compatible with both Java 5 andJava 6 without having to change theJAXP-JARV-adapter JAR file, one cansimply add another JAR file containinga correctjavax.xml.validation.SchemaFactoryService Provider file.


I can't help you with the JAXP validation API, but Nux provides a class that can validate virtually every type of schema known to man. Regarding RELAX NG schemata, use this factory method to create the relevant validator object.