Skip to content
Stefan Bodewig edited this page Jan 23, 2016 · 6 revisions

Simplified XSLT Transformations

The org.xmlunit.transform package or Org.XmlUnit.Transform namespace contain a Transformation class that tries to hide away the complexity of javax.xml.transform (and to a lesser degree of System.Xml.Xsl).

It's main purpose inside of XMLUnit is to make testing XSLT stylesheets easier.

Something like

Source control = ...
Source test = Input.byTransforming(someInputDocument)
    .withStylesheet(sourceOfStylesheet).build();

assertThat(test, CompareMatcher.isIdenticalTo(control));

or

ISource control = ...
ISource test = Input.ByTransforming(someInputDocument)
    .WithStylesheet(sourceOfStylesheet).Build();

Assert.That(test, CompareConstraint.IsIdenticalTo(control));

can be used to easily assert that applying a certain stylesheet to a given source document yields an expected result.

The Transformation class expects a stylesheet and source and provides methods to transform to a TraX Result or .NET Stream with convenience methods for transforming to a string or DOM document. It also provides access to some features of the underlying implementation.

Transformation Builder

Transform in the builder package/namespace provides a fluent API on top of the Transformation class. A transformation of a file to a different file could be written as

StreamResult r = new StreamResult(new File(TARGET));
Transform
    .source(Input.fromFile(SOURCE).build())
    .withStylesheet(Input.fromFile(STYLESHEET).build())
    .build()
    .to(r);

or

using (FileStream fs = new FileStream(SOURCE, FileMode.OpenOrCreate, FileAccess.Write)) {
    Transform
        .Source(Input.FromFile(SOURCE).Build())
        .WithStylesheet(Input.FromFile(STYLESHEET).Build())
        .Build()
        .To(fs);
}
Clone this wiki locally