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+).

The Javadocs of that package talk about supporting schema languages other than W3C's XML Schema and in particular talk a lot about RELAX NG.

The way to use RELAX NG with the new XMLUnit Validator class is pretty straight forward in code:

using org.custommonkey.xmlunit.jaxp13.Validator;

        Validator v = new Validator(javax.xml.XMLConstants.RELAXNG_NS_URI);
        v.addSchemaSource(new StreamSource(new File(".../Book.rng")));
        StreamSource s =
            new StreamSource(new File(".../BookXsdGeneratedNoSchema.xml"));
        assertTrue(v.isInstanceValid(s));

this is from a JUnit test. Unfortunately getting JAXP to use a SchemaFactory that supports RELAX NG is less simple.

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.

blog comments powered by Disqus

path: /en/oss/XMLUnit | # | Writebacks