当外部文档不可用时,我需要 XSLT 停止处理并出现错误。据我所知,
<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>
将被显示,但由于某些原因,
最佳答案
你的想法是对的,但是......
如果您的 XSLT 处理器实现了 XSLT 1.0,从技术上讲它不必终止。注意在 spec for xsl:message
中使用了应该而不是必须 :
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 changes从应该到必须:
If the effective value of the terminate attribute is yes, then the processor must terminate processing after sending the message.
另请注意,xsl:message
语句的执行顺序取决于处理器;在日志中查找 xsl:message
输出时请记住这一点。
最后,您在 XSLT 2.0(error() 函数)和 XSLT 3.0(xsl:try and xsl:catch)下有一些额外的异常处理选项。
关于xml - 从 XSLT 中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644704/