XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / message

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

Instruction xsl:message is mainly used for debugging or error messages alongside or instead of result generation. It can optionally stop stylesheet processing.

Classification

Instruction

Function group

Messages during processing

Introduced

XSLT 1.0

Position in the stylesheet and permitted contents:

xsl:message may appear anywhere inside template rules. Its optional body is a sequence constructor containing Literal Result Elements and XSLT instructions.

Attributes:

Standard attributes apply. Instruction xsl:message has optional attribute terminate. XSLT 2.0 added optional attribute select.

select

Value

xpath-expression

Usage

Optional

Introduced

XSLT 2.0

Attribute select defines message content instead of or in addition to the permitted sequence constructor.

Note: This attribute does not exist in XSLT 1.0.

terminate

Value

{yes | no} (AVT since XSLT 2.0)

Usage

Optional

Introduced

XSLT 1.0

Attribute terminate controls whether the processor stops after an error message. Its default is "no". With value "yes", processing stops after the message. With value "no", it continues.

Note: Since XSLT 2.0, an AVT can determine the value at runtime.

Purpose:

Instruction xsl:message creates a secondary document node when instantiated, usually on error. (Note: XSLT 1.0 creates a text node; otherwise the procedure is the same.) The sequence constructor defines the content, either literal or generated at runtime by internal XSLT instructions.

If attribute select is present, its XPath expression is evaluated and its sequence precedes that of the constructor. Thus attribute select behaves like an instruction xsl:copy with the same attribute select at the start of the constructor:

<xsl:message select="//meldung" terminate="no"/>

is therefore equivalent to:

<xsl:message terminate="no">
  <xsl:copy select="//meldung"/>
</xsl:message>

The document node created by xsl:message has no URI and cannot be considered a result tree (result tree).

This outputs relevant error information. A message from xsl:message never enters the result tree, even if one is generated with terminate="no". Instead, application-dependent output is used, usually a console message, possibly a popup or log entry.

Note: Unlike other XSLT instructions, xsl:message permits both attribute select and an internal sequence constructor. Elsewhere they are mutually exclusive, as with xsl:attribute, xsl:comment, xsl:param, xsl:namespace, xsl:perform-sort, xsl:processing-instruction, xsl:sort, xsl:value-of, xsl:variable and xsl:with-param.

Example – Message output and termination:

<xsl:template match="adressen">
  <xsl:for-each select="adresse">
    <b>Name: </b><xsl:value-of select="name" /><br />
    <b>eMail: </b>
    <xsl:if test="email=''">
      <xsl:message terminate="yes">E-Mail-Adresse fehlt für <xsl:value-of select="name"/>!</xsl:message>
    </xsl:if>
    <xsl:value-of select="email" /><br />
    <hr />
  </xsl:for-each>
</xsl:template>

Names and email addresses are output from an address list. If an email is missing, xsl:message stops processing with an error because of terminate="yes", producing a secondary text message with the name. No result document is generated.

Element definition:

XSLT 1.0:

<!-- Category: instruction -->
<xsl:message 
     terminate = "yes" | "no">

     <!-- Content: template -->
</xsl:message>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:message
     select? = expression 
     terminate? = { "yes" | "no" }>

     <!-- Content: sequence-constructor -->
</xsl:message>

Note: Attribute terminate is an AVT since XSLT 2.0.

   

  

back to top