As previously hinted or even promised XMLUnit for Java 1.2 is out.

We've added a few examples that can be used as starting points for more advanced customizations of XMLUnit's difference engine and a new subsystem that uses JAXP 1.3's validation framework, which - in theory - allows validation of other Schema languages as well.

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

Deepal Jayasinghe provides example code for XML Schema Document validation:

If you are looking for a tool to validate your XSD against the http://www.w3.org/2001/XMLSchema.xsd Then you can build your own tool using the following code.

An alternative would be XMLUnit's svn trunk and the following code

import org.custommonkey.xmlunit.jaxp13.Validator;

        Validator v = new Validator();
        v.addSchemaSource(new StreamSource(new File(...)));
        assertTrue(v.isSchemaValid());

XMLUnit 1.2 with support for javax.xml.validation (which Deepal uses as well) shouldn't be too far away.

In theory this should work for Relax NG as well, for a reality check see this.

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

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.

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

When I "took over" XMLUnit development my hope was to evolve the .NET and Java versions side by side, but as usual I overestimated the time I have available and underestimated the effort it would take. As a result the Java version is now even more ahead of the .NET version than it was eighteen months ago.

Lately I needed to perform a bunch of XPath assertions in the unit tests of a .NET project and this was the first time I actually used XMLUnit.NET. We are using NUnit 2.4 and I quickly learned that the latest released XMLUnit.NET wouldn't work. Fortunately all it took was to recompile XMLUnit.

Since I've been asked for a XMLUnit version that worked with more modern versions of NUnit a few times, just grabbing the source and recompiling doesn't seem to be something people want to do.

Without further ado, here is that recompiled version. Please use the checksums and PGP signature from http://xmlunit.sourceforge.net/checksums/ to verify the integrity of the archive you've just downloaded.

XMLUnit.NET 0.3.1 is a version of XMLUnit.NET that has been compiled against NUnit 2.4 and should work on any modern version of Mono as well as all Microsoft .NET frameworks (only tested on 1.1 and above, though).

Apart from being compiled against a more modern version of NUnit, only a few deprecation warnings have been fixed when compared to XMLUnit.NET 0.3. No features have been added, no bugs have been fixed (there haven't been any bug reports anyway). Unfortunately no documentation has been added, either.

If you want to help improving XMLUnit (any platform version), your input is more than welcome. Please come over to the mailing list and raise your hand.

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

For some reason the Sun engineers decided it would be a good idea to base the XSLT engine of JDK 1.5 on Apache Xalan XSLTC. I'm not familiar enough with Xalan-J development to know whether XSLTC seemed to be stable back then or whether it would be a good choice today, but I do know that the specific version used is buggy. Too buggy to use in any environment.

We've had out share of XSLTC related bug reports in Ant (like this one) and it completely breaks XPath assertions in XMLUnit prior to 1.1 because it chokes on this trivial stylesheet:

<xsl:stylesheet ...>
  <xsl:preserve-space elements="*"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8"/>
  <xsl:template match="/">
    <xpathResult>
      <xsl:apply-templates select="XPATH_EXPRESSION" mode="result"/>
    </xpathResult>
  </xsl:template>
  <xsl:template match="*" mode="result">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

which is used by SimpleXPathEngine to get the result of selecting XPATH_EXPRESSION from a document.

And now it strikes again in XMLUnit see this bug report or current Gump builds until I get around fixing it. XMLUnit uses an even simpler stylesheet to get rid of element content whitespace if you ask it to:

<xsl:stylesheet ...>
  <xsl:output method="xml" version="1.0" indent="no"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/"><xsl:copy-of select="."/></xsl:template>
</xsl:stylesheet>

This fails to strip element content whitespace if you use XML namespaces in your documents.

A bug in XSLTC that has been fixed in December 2005 and has been reported to Sun in April 2006 is still alive - at least in Update 11 of Java 5 which is what you get on Ubuntu, I haven't checked update 12, yet.

Back to thinking about a workaround for this issue, other than telling people to use Xalan-J or GNU JAXP or any other working implementation instead.

Of course it works for any version of JDK 1.4 or 1.6.

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