Skip to content

XSLT <xsl:message>

How to use the XSLT <xsl:message> element to log errors, debug transformations, and control processing flow with the terminate attribute.

XSLT <xsl:message>

The <xsl:message> element writes a message to the output. This element is primarily used to report errors.

This element can contain almost any other XSL element (<xsl:text>, <xsl:value-of>, etc.).

The terminate attribute gives you the choice to either quit or continue the processing when an error occurs.

Syntax

<xsl:message terminate="yes|no">  
	<!-- Content:template -->
</xsl:message>

Attributes

Attribute Value Required/Optional Description
terminate yes|no Optional "yes" terminates the processing after the message is written to the output.
"no" continues the processing after the message is written to the output. Default is "no".

Example

Consider the following example about a bookstore:

books.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Tips</title>
		<author>Tom Nolan</author>
		<year>2022</year>
		<price>39.99</price>
	</book>
	<book category="web">
		<title lang="en">Learn XML</title>
		<author>Tutorial Reference</author>
		<year>2022</year>
		<price>9.99</price>
	</book>
</bookstore>

Check if author is an empty string. If yes, we quit the XSL processor and display a message:

books.xslxml
<?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>
  <xsl:for-each select="bookstore/book">
    <p>Title: <xsl:value-of select="title"/><br/>
    Artist:
    <xsl:if test="author=''">
      <xsl:message terminate="yes">
        Error: Author is an empty string!
      </xsl:message>
    </xsl:if>
    <xsl:value-of select="author"/>
    </p>
  </xsl:for-each>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
Navigation