Sunday, February 7, 2010

Maven: Testing XSLT Transforms

Looking for a quick way to test a XSLT Transformations, I decided to check out the XML Maven Plugin hosted at the Codehaus Mojo Project.

For illustrative purposes, I want to turn a XML document of people into HTML:

<people>
<person>
<firstName>Ronnie</firstName>
<lastName>Gardocki</lastName>
</person>
<person>
<firstName>Shane</firstName>
<lastName>Vendrel</lastName>
</person>
</people>

With the following simple stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
<title>People in HTML</title>
<body>
<h1>People in HTML</h1>

<xsl:apply-templates/>

</body>
</head>
</html>
</xsl:template>

<xsl:template match="person">
<table>
<th>Person</th>
<td><xsl:value-of select="firstName"/></td>
<td><xsl:value-of select="lastName"/></td>
</table>
</xsl:template>

</xsl:stylesheet>

The xml:transform plugin goal takes, among others, a dir parameter to configure where the files are you want to transform, and a stylesheet parameter describing the XSLT file to use to transform the documents found in the directory. Here I'm also overriding the default output directory for the transformed documents, target/generated-resources/xml/xslt:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/xml</dir>
<stylesheet>src/main/xslt/${stylesheet}</stylesheet>
<outputDir>target</outputDir>
</transformationSet>
</transformationSets>
</configuration>
</plugin>

To run an example with the configuration above, I pass in the name of the stylesheet to use which is located in src/main/xslt. I would of liked to have been able to pass in a specific document to transform, but was not able to find a way to configure the plugin to do so.

$ mvn xml:transform -Dstylesheet=people-to-html.xsl
The relevant output provided by the plugin:
[INFO] [xml:transform]
[INFO] Transforming file: /home/prystasj/workspace/prystasj/xslt-test/src/main/xml/people.xml
[INFO] Transformed 1 file(s).

The transform produces this simple HTML:

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>People in HTML</title>
<body>
<h1>People in HTML</h1>

<table>
<th>Person</th><td>Ronnie</td><td>Gardocki</td>
</table>

<table>
<th>Person</th><td>Shane</td><td>Vendrel</td>
</table>

</body>
</head>
</html>

If we want to try out a new transform, we can drop a new stylesheet and example source documents in the appropriate directories, hopefully creating some quick turnaround time for testing some transforms.

1 comment:

  1. very useful article man, these tips are simply great. by the way I have also blogged my experience as comparator and comparable in java with example, any feedback or suggestion will be highly welcomed. Thanks

    ReplyDelete