Tracing the Flow of Your Stylesheet Through Its Input Document

Problem

You want to trace your stylesheet’s navigation through the XML document.

Solution

You should first consider the trace options available in your XSLT processor. Saxon has a -t option that displays timing information about various processing stages and a -T option that causes the output of trace information. Xalan has -TT, which traces the templates as they are called; -TG, which traces each generation event; -TS, which traces each selection event; and -TTC, which traces template children as they are called.

If your processor does not support trace output or you need higher degrees of control over the output, you can consider a solution based on xsl:message. With xsl:message, it is easy to generate debug output that lets you trace the flow of control through the stylesheet. It is also useful to trace the flow of the stylesheet through the document. Here is a utility you can import into any stylesheet for this purpose:

<!-- xtrace.xslt --> <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="debugOn" select="false( )"/> <xsl:template match="node( )" mode="dbg:trace" name="dbg:xtrace"> <xsl:param name="tag" select=" 'xtrace' "/> <xsl:if test="$debugOn"> <xsl:message> <xsl:value-of select="$tag"/>: <xsl:call-template name="dbg:expand-path"/> </xsl:message> </xsl:if> </xsl:template> <!--Expand the xpath to the current ...

Get XSLT Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.