XML technologies / XSLT / XSLT Cookbook / Testing and debugging / xsl:message: effective use
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to inspect a stylesheet to learn why it does not behave as expected.
Caution!
A common problem is running a stylesheet and seeing no output, or only text without elements. In my experience, this almost always indicates a namespace problem. It is easy to forget namespace prefixes in match attributes on xsl:template, or the namespace URI in the document differs from that in the stylesheet. Remember that the namespace URI matters, not the prefix. A single differing letter causes a problem. For long URIs, I copy the namespace declaration from the document to the stylesheet to ensure an exact match.
Solution
XSLT 1.0
The simplest tool in your debugging arsenal is xsl:message, often used to check whether a particular template executes:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- ... -->
<xsl:template match="someElement[someChild = 'someValue']">
<xsl:message>Matched someElement[someChild = 'someValue']</xsl:message>
<!-- ... -->
</xsl:template>
</xsl:stylesheet>Displaying relevant data makes this technique more useful. Label output with recognizable descriptive text so that it can be distinguished from other uses of xsl:message and reveal when a message executes but produces an empty result:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="someElement[someChild = 'someValue']">
<xsl:param name="myParam"/>
<!-- Das ist keine wirksame Debugging-Technik. Wenn Sie einen Test ausführen und nichts sehen, kann das daran liegen, dass das Template keine Übereinstimmung ergab, oder daran, dass es eine Übereinstimmung mit einem leeren $myParam gab. -->
<xsl:message><xsl:value-of select="$myParam"/></xsl:message>
</xsl:template>
<xsl:template match="someElement[someChild = 'someOtherValue']">
<xsl:param name="myParam"/>
<!-- Das hier ist besser -->
<xsl:message>Matched someElement[someChild = 'someOtherValue']</xsl:message>
<xsl:message>$myParam=[<xsl:value-of select="$myParam"/>]</xsl:message>
</xsl:template>
</xsl:stylesheet>Use a debugging parameter to keep xsl:message instrumentation in the stylesheet. Put the parameter in your own namespace when sharing the code, to avoid conflicts with other people’s debugging facilities:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dbg="http:www.ora.com/XSLTCookbook/ns/debug">
<xsl:param name="dbg:debugOn" select="false( )"/>
<xsl:template match="someElement[someChild = 'someValue']">
<xsl:param name="myParam"/>
<xsl:if test="$dbg:debugOn">
<xsl:message>Matched someElement[someChild = 'someValue']</xsl:message>
<xsl:message>$myParam=[<xsl:value-of select="$myParam"/>]</xsl:message>
</xsl:if>
</xsl:template>
</xsl:stylesheet>XSLT 2.0
Version 2.0 adds two useful improvements. First, xsl:message now has a selection attribute, usable instead of sequence-constructor syntax and convenient for short messages. Second, attribute terminate can now be an attribute value template, simplifying global changes to termination behavior and assertion-message generation in stylesheets.
<!-- Ausgeben des Werts von $foo und terminieren, wenn er negativ ist -->
<xsl:message select=" 'foo=', $foo " terminate="{ if ($foo lt 0) then 'yes' else 'no'}" />Depending on the XSLT processor, you may be able to exploit attribute use-when, standard on every XSLT 2.0 instruction. Test a custom debugging system property. In the Saxon implementation of system-property( ), a name outside a namespace, without a prefix, is interpreted as a reference to a Java system property, whose value is returned if it exists. This provides conditionally compiled messages.
<xsl:message use-when="system-property('debug_on') = 'yes' ">
<xsl:text>Debug mode is on!</xsl:text>
</xsl:message>Set property debug-on when starting Saxon with Java:
java -Ddebug_on=yes -jar c:\saxon\saxon8.jar test.xml test.xslt
Discussion
XSLT 1.0
Before debuggers, people used print statements. Some interactive XSLT debuggers now exist, but to my knowledge none is available free.
Besides the above use of xsl:message, consider assertions ( assertions) to test preconditions, postconditions or invariants that must hold at a point in the stylesheet:
<xsl:if test="debugOn">
<xsl:if test="Test einer Invarianten einfügen">
<xsl:message terminate="yes">Meldung über eine Verletzung oder einen Fehler.</xsl:message>
</xsl:if>
</xsl:if>Such tests often use terminate="yes" because these errors are unrecoverable by definition.
When debugging with xsl:message, consider the output destination, which varies by execution environment. A browser transformation may not show it at all. Test stylesheets with a command-line processor before moving to the actual target environment.
For XML or HTML output, an alternative to xsl:message is adding debugging comments using xsl:comment to the result document. In particular, begin every template with an xsl:comment to trace output back to the templates that generated it:
<xsl:template match="*">
<xsl:comment>Generiert durch Übereinstimmung mit Platzhalter</xsl:comment>
...
</xsl:template>
...
<xsl:template match="*" mode="foo">
<xsl:comment>Generiert durch Übereinstimmung mit Platzhalter und mode=foo</xsl:comment>
...
</xsl:template>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de