Capture xsl:message output from Saxon

We are using the Saxon Xslt processor on a Java project to produce ISO20022 XML files. The grouping facility provided by XPath 2.0 is proving a real hit. In this post we give a quick hint on capturing xsl:message output.

The conditional capabilities of Xsl allow us to define rules within our transformations. In the event of rule failures we can report back to Java using the xsl:message statement. For various timing reasons Saxon doesnt report the xsl:message statements via the ErrorListener interface (well not directly).

The two samples below demonstrate how to capture the xsl:message output. The samples are exactly the same except one uses a Transfomer constructed from a Templates object and the other uses an XMLFilter. To begin here is a simple xsl file.

Here is a JUnit test case that uses the Transformer constructed via the Templates object.

And next we have the same functionality instead we use an XMLFilter.

Both samples produce identical output, illustrated below. The ErrorListener assigns the text received from the xsl:message statement to the message member variable. This allows the assertions at the end of the unit tests to succeed.

Tagged with: , ,
Posted in Java

Leave a Reply

Your email address will not be published.