9

I'm trying to capture xsl:message in java when calling my transform. Below is a snippet of my code.

        final ArrayList<TransformerException> errorList = new ArrayList<TransformerException>();
        ErrorListener errorListener = new ErrorListener() {
          @Override
          public void warning(TransformerException e) throws TransformerException {
            //To change body of implemented methods use File | Settings | File Templates.
            log.error(e.getMessage());
            errorList.add(e);
          }

          @Override
          public void error(TransformerException e) throws TransformerException {
            //To change body of implemented methods use File | Settings | File Templates.
            log.error(e.getMessage());
            errorList.add(e);
          }

          @Override
          public void fatalError(TransformerException e) throws TransformerException {
            //To change body of implemented methods use File | Settings | File Templates.
            errorList.add(e);
            throw e;
          }
      };
      ...
      try
      {
        transformer.setErrorListener(errorListener);
        newDoc = transform(transformer, oldDoc);
      }
      catch (TransformerException e) {
        log.error("Problem transforming normalized document into PUBS-XML", e);
        throw e;
      }

Unfortunately this is not working.

Is there a better way?

Thanks in advance!

3
  • Which xslt processor are you using? Commented Jan 14, 2011 at 20:32
  • believe I'm using Xalan with javax.xml.transform.*. My stylesheet are version 2. Commented Jan 14, 2011 at 20:59
  • Correction. processor is Saxon. Answer below is the solution for capturing xsl:message output. Commented Jan 17, 2011 at 17:18

1 Answer 1

16

If you are using Saxon, then you may need to set the message emitter using setMessageEmitter().

https://www.saxonica.com/html/documentation10/javadoc/net/sf/saxon/trans/XsltController.html#setMessageEmitter-net.sf.saxon.event.Receiver-

public void setMessageEmitter(Receiver receiver)

Set the Receiver to be used for xsl:message output.

Recent versions of the JAXP interface specify that by default the output of xsl:message is sent to the registered ErrorListener. Saxon does not implement this convention. Instead, the output is sent to a default message emitter, which is a slightly customised implementation of the standard Saxon Emitter interface.

This interface can be used to change the way in which Saxon outputs xsl:message output.

Michael Kay has explained why Saxon doesn't output xsl:message according to the JAXP interface, and has suggested two options for obtaining the output:

ErrorListener was something that was introduced to JAXP at a rather late stage (one of many regrettable occasions where the spec was changed unilaterally to match the Xalan implementation), and I decided not to implement this change as a default behaviour, because it would have been disruptive to existing applications.

In Saxon, xsl:message output is directed to a Receiver, which you can nominate to the Transformer:

((net.sf.saxon.Controller)transformer).setMessageEmitter(....)

If you want to follow the JAXP model of sending the output to the ErrorListener, you can nominate a Receiver that does this:

((net.sf.saxon.Controller)transformer).setMessageEmitter(new net.sf.saxon.event.MessageWarner())

Sign up to request clarification or add additional context in comments.

3 Comments

net.sf.saxon.event.MessageWarner is the right class for older versions (9.3 and earlier?) of Saxon, and Saxon-CE (which is based on the 9.3 codebase). It is net.sf.saxon.serialize.MessageWarner in newer versions of Saxon. Note that this sends messages to the warning() method of ErrorListener, unless terminate="yes" in which case it sends them to the error() method.
In case anyone is interested: I made a wrapper for Saxon's TransformerFactory here: github.com/nverwer/cocooncomponents/blob/master/src/org/apache/…
“Recent versions of the JAXP interface specify that by default the output of xsl:message is sent to the registered ErrorListener.” ⇒ Where is this specified? I have checked JSR 206 - JAXP 1.6 and the Javadoc for Transformer and ErrorListener and couldn’t find any reference to the treatment of xsl:message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.