The XSLT <xsl:message> element is used to display the error message and help to debug the XSLT processing. It is similar to JavaScript alerts. This element buffers a message to XSLT processor which terminates the processing and sends a message to the caller application to show an error message.
<xsl:message terminate = "yes" | "no"> </xsl:message>
Terminate: It specifies if the transformation should terminate upon executing this instruction or not. When the terminate attribute is set to "yes", the content of the element is displayed as the part of the system-level error message, and the transformation terminates. When it is set to "no", the transformation proceeds, ignoring the error message. By default value is ?no?.
Let's take an example to create a <student> element with its attribute "id" and its child <firstname>, <lastname>, <nickname>, and <salary> by iterating over each employee. It checks key as firstname to be present and then prints the employee's details, otherwise displays an error message.
Employee.xml
<?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> <class> <employee id = "001"> <firstname></firstname> <lastname>Gupta</lastname> <nickname>Raju</nickname> <salary>30000</salary> </employee> <employee id = "024"> <firstname>Sara</firstname> <lastname>Khan</lastname> <nickname>Zoya</nickname> <salary>25000</salary> </employee> <employee id = "056"> <firstname>Peter</firstname> <lastname>Symon</lastname> <nickname>John</nickname> <salary>10000</salary> </employee> </class>
Employee.xsl
<?xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> <xsl:template match = "/"> <html> <body> <h2>Employee</h2> <table border = "1"> <tr bgcolor = "pink"> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Salary</th> </tr> <xsl:for-each select = "class/employee"> <xsl:if test = "firstname = ''"> <xsl:message terminate = "yes">A first name field is empty. </xsl:message> </xsl:if> <tr> <td><xsl:value-of select = "@id"/></td> <td><xsl:value-of select = "firstname"/></td> <td><xsl:value-of select = "lastname"/></td> <td><xsl:value-of select = "nickname"/></td> <td><xsl:value-of select = "salary"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Output:
Employee.xsl
<?xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> <xsl:template match = "/"> <html> <body> <h2>Employee</h2> <table border = "1"> <tr bgcolor = "pink"> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Nick Name</th> <th>Salary</th> </tr> <xsl:for-each select = "class/employee"> <xsl:if test = "firstname = ''"> <xsl:message terminate = "no">A first name field is empty. </xsl:message> </xsl:if> <tr> <td><xsl:value-of select = "@id"/></td> <td><xsl:value-of select = "firstname"/></td> <td><xsl:value-of select = "lastname"/></td> <td><xsl:value-of select = "nickname"/></td> <td><xsl:value-of select = "salary"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Output: