Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

xslt - Capture xsl:message output in java

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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())


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...