Fork me on GitHub

Transforming XML files

To transform XML files by applying XSLT stylesheets, use the "xml:transform" goal is used. It is configured through a section like the following in your POM:

  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xml-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>transform</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <transformationSets>
            <transformationSet>
              <dir>xml</dir>
              <stylesheet>it4.xsl</stylesheet>
            </transformationSet>
          </transformationSets>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>

The example would transform all files in the directory "xml" by applying the stylesheet "it4.xml". The output would be written to the directory "$project.build.directory/generated-resources/xml/xslt" (usually "target/generated-resources/xml/xslt").

Goal properties

The "xml:transform" goal offers the following configurable properties:

Property Name Description
basedir The base directory, which is used for interpreting relative paths. Defaults to the project directory, in which the POM resides.
catalogHandling How to handle entities which cannot be resolved from the catalogs. There are 3 possible values:  
* 'passThrough' indicates that the entity resolver should attempt to resolve system and public IDs through default mechanisms, typically by resolving URI formatted system IDs. 
* 'local' indicates that the entity resolver should behave as for 'passThrough' but only where URI's are 'file' format URIs. 
* 'strict' indicates that any entity which cannot be found in a catalog should generate an error.
catalogs A set of catalog files, which configure the entity resolver. For example, it allows to map public ID's or external URL's to local files. Multiple catalog files are supported. In other words, to configure a single catalog file, you would need a section like this: 
 
    <catalogs> 
        <catalog>mycatalog.xml</catalog> 
    </catalogs> 
 
The interpretation of catalog files is done by the Apache XML resolver. See this article for details on catalog files and their formats.
forceCreation If 'true', no up-to-date check is performed and the transformation is always performed. Defaults to "$xml.forceCreation" or 'false'.
transformationSets A transformation set configures a set of XML files, which are transformed by applying a common XSLT stylesheet. If you want apply multiple stylesheets, use multiple transformation sets. See the above example, which specifies a single transformation The various child elements of a <transformationSet> section listed in the following section.

Transformation Set Configuration

A transformation set is configured through several child elements:

Element Name Description
attributes Specifies attributes, which are being set on the transformer factory by invoking the setAttribute(String, Object) method.
addedToClasspath Specifies, whether the output directory is being added to the to the classpath. Defaults to true.
dir Sets the name of a directory, which is scanned for the files, which are being validated.
the directory given by "dir". This element uses the normal maven includes element convention to support multiple patterns: 
 
    <excludes> 
        <exclude>[file pattern #1]</exclude> 
        <exclude>[file pattern #2]</exclude> 
        ... 
    </excludes> 
features Specifies features, which are being set on the transformer factory by invoking the setFeature(String,boolean) method.
includes Specifies a pattern of files, which are being included. By default, all files are included. The pattern is relative to the directory given by "dir". This element uses the normal maven includes element convention to support multiple patterns: 
 
    <includes> 
        <include>[file pattern #1]</include> 
        <include>[file pattern #2]</include> 
        ... 
    </includes> 
otherDepends A list of of extra input files or URLs to consider during the up-to-date check, useful for when resolving remote files. By default it always considers: 1. input files, 2. XSLT file, 3. catalog files, and 4. the pom.xml file of the project executing this plugin.
outputDir Specifies the name of the output directory, where the generated files are being placed. Defaults to "$project.build.directory/generated-resources/xml/xslt". Usually, this is equivalent to "target/generated-resources/xml/xslt".
outputProperties A list of name/value pairs, which are used to invoke the method setOutputProperty(...) on the transformer. For details, see this example.
skipDefaultExcludes Specifies whether the maven's default exludes should NOT be added to the excludes list.
stylesheet Specifies the stylesheet, which is being used for transforming the fileset. A stylesheet must be specified, there is no default.
xincludeAware If 'true' enables XInclude support. Disabled by default.