Thank you all the same, Michael Kay.
 
Finally, I found an example of setMessageEmitter :
 
System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer(new StreamSource(xsltFile));
transformer.setParameter("documentType", "Document");
transformer.setURIResolver(zipUIRResolver);
net.sf.saxon.Controller controller = (net.sf.saxon.Controller)transformer;
net.sf.saxon.event.Receiver receiver = new XSLTMessageReceiver();
controller.setMessageEmitter(receiver);  
transformer.transform(new StreamSource(xmlFile), new StreamResult(System.out));
 
 
XSLTMessageReceiver.java
 
import net.sf.saxon.event.PipelineConfiguration;
import net.sf.saxon.trans.XPathException;
/**
 * A class used to redirect output from xsl:message elements to OpenSyncro's
 * Transfer log.
 * 
 * Regular xsl:message's are written as debug (info) level messages while
 * xsl:message's with terminate="yes" attribute are written as error level
 * messages.
 */
public class XSLTMessageReceiver implements net.sf.saxon.event.Receiver {
//private MessageLogger logger;
//private Object caller;
    
    /** 
     * Contains information on whether the xsl:message element had attribute
     * terminate="yes" (ReceiverOptions.TERMINATE). Given by Saxon only at
     * the startDocument() call.
     */
private int startElementProperties;
    
    // Specified as required by getter/setter methods in the Receiver interface
    private PipelineConfiguration pipelineConfig;
    private String systemId;
/**
 * Create new XSLTMessageReceiver that will output xsl:messages to Transfer log. 
 * 
 * @param logger <code>MessageLogger</code> used.
 * @param caller <code>Object</code> reference to be used in the Transfer log.
 */
public XSLTMessageReceiver (){
//this.logger = logger;
//this.caller = caller;
        this.startElementProperties = 0;
}
    public void startDocument(int properties) throws XPathException {
        this.startElementProperties = properties;
    }
    public void characters(CharSequence chars, int locationId, int properties) throws XPathException {
        if(chars.toString().startsWith("progress:text:p")) { 
        
        }else{
         //System.out.println(chars.toString());
        }
        //if((this.startElementProperties & ReceiverOptions.TERMINATE) != 0) {
            //logMessageLevel = MessageLogger.ERROR;
        //} else {
            //logMessageLevel = MessageLogger.DEBUG;
        //}
        
        //logger.logMessage("xsl:message: " + chars, caller, logMessageLevel);
    }
    // Obligatory PipelineConfiguration and SystemId getter/setter methods implemented
    
    public void setPipelineConfiguration(PipelineConfiguration pipe) {
        this.pipelineConfig = pipe;
    }
    public PipelineConfiguration getPipelineConfiguration() {
        return pipelineConfig;
    }
    public void setSystemId(String systemId) {
        this.systemId = systemId;
    }
    public String getSystemId() {
        return systemId;
    }
    // The following methods are intentionally left no-op
    
    public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) throws XPathException {}
    public void comment(CharSequence content, int locationId, int properties) throws XPathException {}
    public void namespace(int namespaceCode, int properties) throws XPathException {}
    public void processingInstruction(String name, CharSequence data, int locationId, int properties) throws XPathException {}
    public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException {}
    public void startContent() throws XPathException {}
    public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {}
    public void endElement() throws XPathException {}
    public void endDocument() throws XPathException {}
    public void open() throws XPathException {}
    public void close() {}
}
 
 
 
 
 
 
 
2011-06-09

dba03

发件人: Michael Kay
发送时间: 2011-06-07  19:51:15
收件人: saxon-help
抄送:
主题: Re: [saxon] any example about setMessageEmitter?
On 07/06/2011 10:25, dba03 wrote:
Hi,
There are three type of <xsl:message>,
<xsl:message terminate="no">a:aaaaaaaa</xsl:message>
<xsl:message terminate="no">b:testesttest</xsl:message>
<xsl:message terminate="no">c:testesttest</xsl:message>
I want to filter the message beginning with "a".
How can I do it?
 
I have read the following,
http://stackoverflow.com/questions/4695489/capture-xslmessage-output-in-java
setMessageEmitter(Receiver receiver)
 
But I still didn't know how to do it? Any simple examples?
Thanks.
 

 

I would recommend using the s9api interface, which layers on top of this underlying mechanism; in particular, it delivers each message in a single call as an XML document. (The lower-level interface invokes a stream of event methods: startDocument, characters, endDocument).

You write a MessageListener as described in

http://www.saxonica.com/documentation/javadoc/net/sf/saxon/s9api/MessageListener.html

with the single method

void message(XdmNode content,
             boolean terminate,
             SourceLocator locator)

and register it with the XsltTransformer using setMessageListener.

Then to test the content of the message, in the case where it's a simple string as you describe, use XdmNode.getTypedValue().toString()

Michael Kay
Saxonica