| <xsl:message> | |
| Sends a message. How the message is sent can vary from one XSLT processor to the next, but it's typically written to the standard output device. This element is useful for debugging stylesheets. | |
| Category | |
|
Instruction |
|
| Required Attributes | |
|
None. |
|
| Optional Attributes | |
|
|
| Content | |
|
An XSLT template. |
|
| Appears in | |
|
<xsl:message> appears inside a template. |
|
| Defined in | |
|
XSLT section 13, Messages. |
|
| Example | |
|
Here's a stylesheet that uses the <xsl:message> element to trace the transformation of an XML document. We'll use our list of recently purchased albums again: <?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list> We'll list all of the purchased albums in an HTML table, with the background color of each row alternating through various colors. Our stylesheet uses an <xsl:choose> element inside an <xsl:attribute> element to determine the value of the bgcolor attribute. If a given <listitem> is converted to an HTML <tr> with a background color of lavender, we'll issue a celebratory message:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select="list/title"/>
</title>
</head>
<body>
<h1><xsl:value-of select="list/title"/></h1>
<table border="1">
<xsl:for-each select="list/listitem">
<tr>
<td>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 4 = 0">
<xsl:text>papayawhip</xsl:text>
</xsl:when>
<xsl:when test="position() mod 4 = 1">
<xsl:text>mintcream</xsl:text>
</xsl:when>
<xsl:when test="position() mod 4 = 2">
<xsl:text>lavender</xsl:text>
<xsl:message terminate="no">
<xsl:text>Table row #</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text> is lavender!</xsl:text>
</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:text>whitesmoke</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Note that the XSLT specification doesn't define how the message is issued. When we use this stylesheet with Xalan 2.0.1, we get these results: file:///D:/O'Reilly/XSLT/bookSamples/AppendixA/message.xsl; Line 32; Column 51; Table row #2 is lavender! file:///D:/O'Reilly/XSLT/bookSamples/AppendixA/message.xsl; Line 32; Column 51; Table row #6 is lavender! Xalan gives us feedback on the part of the stylesheet that generated each message. Saxon, on the other hand, keeps things short and sweet: Table row #2 is lavender! Table row #6 is lavender! For variety's sake, here's how XT processes the <xsl:message> element: file:/D:/O'Reilly/XSLT/bookSamples/AppendixA/test4.xml:5: Table row #2 is lavender! file:/D:/O'Reilly/XSLT/bookSamples/AppendixA/test4.xml:9: Table row #6 is lavender! XT gives information about the line in the XML source document that generated the message. |
|