我使用Saxon XSLT版本9.6.0.1。样式表包含以下代码:
...
<xsl:message terminate="yes">errormessage</xsl:message>
...
我的应用程序按预期终止,但有以下异常:
net.sf.saxon.expr.instruct.TerminationException: Processing terminated by xsl:message at line 45 in
at net.sf.saxon.expr.instruct.Message.processLeavingTail(Message.java:253) at net.sf.saxon.expr.instruct.Message.processLeavingTail(Message.java:253)
at net.sf.saxon.expr.instruct.Choose.processLeavingTail(Choose.java:822)
现在我想知道&#34; errormessage&#34;文字实际上是。我可以在stderr上看到它,但需要将它显示给用户或将其放入日志文件中。
如何以编程方式访问消息文本?
答案 0 :(得分:4)
如果您使用Saxon s9api
,请参阅撒克逊资源中的示例文件S9APIExamples
(http://saxonica.com/download/download_page.xml处提供),其示例设置为MessageListener
:< / p>
Processor proc = new Processor(false);
XsltCompiler comp = proc.newXsltCompiler();
String stylesheet =
"<xsl:transform version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\n" +
" <xsl:template name='main'>\n" +
" <xsl:message><msg>Reading http://www.w3.org/TR/xslt20/ ...</msg></xsl:message>/>\n" +
" <exists>\n" +
" <xsl:value-of select=\"doc-available('http://www.w3.org/TR/xslt20/')\"/>\n" +
" </exists>\n" +
" <xsl:message><msg>finishing</msg></xsl:message>\n" +
" </xsl:template>\n" +
"</xsl:transform>";
StringReader reader = new StringReader(stylesheet);
StreamSource styleSource = new StreamSource(reader, "http://localhost/string");
XsltExecutable templates = comp.compile(styleSource);
XsltTransformer transformer = templates.load();
transformer.setInitialTemplate(new QName("main"));
transformer.setMessageListener(
new MessageListener() {
public void message(XdmNode content, boolean terminate, SourceLocator locator) {
System.err.println("MESSAGE terminate=" + (terminate ? "yes" : "no") + " at " + new Date());
System.err.println("From instruction at line " + locator.getLineNumber() +
" of " + locator.getSystemId());
System.err.println(">>" + content.getStringValue());
}
}
);
Serializer out = proc.newSerializer(System.out);
transformer.setDestination(out);
transformer.transform();