I want to throw an exception if one tag doesn't contain an attribute.
In addition to the correct answer of using <xsl:message terminate="yes"/> : In XSLT 3.0 one can use the new instructions <xsl:try ...> and <xsl:catch ...> : http://www.w3.org/TR/xslt-30/#try-catch. In XSLT 2.0 one can also use the standart XPath function error() to terminate the processing.
As of August 2022, the most recent stable version of the language is XSLT 3.0, which achieved Recommendation status in June 2017. XSLT 3.0 implementations support Java, . NET, C/C++, Python, PHP and NodeJS. An XSLT 3.0 Javascript library can also be hosted within the Web Browser.
XSLT is faster and more concise to develop than performing transformations in Java. You can change XSLT without having to recompile the entire application (just re-create EAR and redeploy).
Streaming Transformations for XML (STX) is an XML transformation language intended as a high-speed, low memory consumption alternative to XSLT version 1.0 and 2.0.
Use xsl:message
with terminate="yes"
to achieve effect similar to throwing an exception:
<xsl:if test="(your condition)"> <xsl:message terminate="yes">ERROR: Missing attribute XYZ under <xsl:value-of select="local-name()"/> !</xsl:message> </xsl:if>
This causes the message to be sent to STDERR and terminate processing.
BTW. this is heavily used in Schematron validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With