Pet*_*lka 26
使用xsl:messagewithterminate="yes"来实现类似于抛出异常的效果:
<xsl:if test="(your condition)">
<xsl:message terminate="yes">ERROR: Missing attribute XYZ under
<xsl:value-of select="local-name()"/> !</xsl:message>
</xsl:if>
Run Code Online (Sandbox Code Playgroud)
这会导致将消息发送到STDERR并终止处理.
BTW.这在Schematron验证中被大量使用.
Dim*_*hev 20
除了使用的正确答案<xsl:message terminate="yes"/>:
在XSLT 3.0中,可以使用新的指令<xsl:try ...>和<xsl:catch ...>:http://www.w3.org/TR/xslt-30/#try-catch
在XSLT 2.0中,还可以使用标准XPath函数error()来终止处理.
以下是使用xsl:try和的示例xsl:catch:
<xsl:result-document href="out.xml">
<xsl:variable name="result">
<xsl:call-template name="construct-output"/>
</xsl:variable>
<xsl:try>
<xsl:copy-of select="$result" validation="strict"/>
<xsl:catch>
<xsl:message>Warning: validation of result document failed:
Error code: <xsl:value-of select="$err:code"/>
Reason: <xsl:value-of select="$err:description"/>
</xsl:message>
<xsl:sequence select="$result"/>
</xsl:catch>
</xsl:try>
</xsl:result-document>
Run Code Online (Sandbox Code Playgroud)