从 XSLT 抛出异常 [英] Throwing an exception from XSLT

查看:22
本文介绍了从 XSLT 抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当外部文档不可用时,我需要 XSLT 来停止处理并出现错误.根据我发现的 <xsl:message>tag 似乎是正确的方法,但到目前为止它不起作用.这是我尝试过的:

I need XSLT to stop processing with an error, when an external document is not available. From what I've found out the <xsl:message> tag seems to be the right way to do it, but so far it doesn't work. Here's what I tried:

<xsl:if test="not(document('some_external_doc.xml')//myxpath)">
    <xsl:message terminate="yes">ERROR: Missing element!</xsl:message>
    <h1>Error detected!</h1>
</xsl:if>

检测到丢失的文档/xpath和<h1>将被显示,但由于某种原因,<xsl:message> 的终止属性将被显示.被忽略.转换是在 Railo 中完成的,因此 XSLT 处理器的使用应该是 Java 的默认值,但我无法找到有关 Railo 正在使用的处理器的确切信息.

The missing document/xpath is detected by the <xsl:if> and the <h1> will be displayed, but for some reason the terminate attribute of the <xsl:message> is ignored. The transformation is done in Railo, so the XSLT processor use should be the Java default, but I wasn't able to find something definitive about the processor Railo is using.

推荐答案

您的想法是正确的,但是...

You have the right idea, however...

如果您的 XSLT 处理器实现了 XSLT 1.0,那么它在技术上不必终止.请注意xsl:message 的规范:

If your XSLT processor implements XSLT 1.0, it technically does not have to terminate. Note the use of the word should rather than must in the spec for xsl:message:

如果 terminate 属性的值为 yes,那么 XSLT 处理器应该在发送消息后终止处理.默认的价值是没有.

If the terminate attribute has the value yes, then the XSLT processor should terminate processing after sending the message. The default value is no.

有趣的是,XSLT 2.0改变了应该必须:

如果terminate属性的有效值为yes,则发送消息后,处理器必须终止处理.

If the effective value of the terminate attribute is yes, then the processor must terminate processing after sending the message.

还要注意,xsl:message 语句的执行顺序取决于处理器;在日志中查找 xsl:message 输出时请记住这一点.

Note also that order of execution of xsl:message statements is processor-dependent; keep this in mind when looking for xsl:message output in the logs.

最后,在 XSLT 2.0 (error() 函数)和 XSLT 3.0(xsl:try 和 xsl:catch).

Finally, you have some additional exception processing options under XSLT 2.0 (error() function) and XSLT 3.0 (xsl:try and xsl:catch).

这篇关于从 XSLT 抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆