Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I throw an exception from XSLT?

Tags:

java

xml

xslt

I want to throw an exception if one tag doesn't contain an attribute.

like image 607
Abin Manathoor Devasia Avatar asked Nov 23 '12 05:11

Abin Manathoor Devasia


People also ask

How do you handle exceptions in XSLT?

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.

Is XSLT still a thing?

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.

Is XSLT faster than Java?

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).

What is the alternative for XSLT?

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.


1 Answers

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.

like image 51
Petr Kozelka Avatar answered Oct 23 '22 22:10

Petr Kozelka