XML technologies / XSLT / XSLT Cookbook / Testing and debugging / Tracing stylesheet flow through its input
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to trace ( trace) how a stylesheet navigates an XML document.
Solution
XSLT 1.0
First check the tracing options available in your XSLT processor. Saxon provides option -t for timing processing steps, and option -T for trace output. Xalan provides -TT to trace invoked templates, -TG for all generation events, -TS for selection events, and -TTC to trace invoked template children.
If your processor has no tracing or you need more control, consider a solution based on xsl:message. Using xsl:message makes it easy to trace stylesheet control flow. Tracing its path through the document is useful too. Import this utility into any stylesheet:
<!-- 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>
<!-- Erweitere den xpath auf den aktuellen Knoten -->
<xsl:template name="dbg:expand-path">
<xsl:apply-templates select="." mode="dbg:expand-path"/>
</xsl:template>
<!-- Wurzel -->
<xsl:template match="/" mode="dbg:expand-path">
<xsl:text>/</xsl:text>
</xsl:template>
<!-- Oberster Knoten -->
<xsl:template match="/*" mode="dbg:expand-path">
<xsl:text>/</xsl:text><xsl:value-of select="name( )"/>
</xsl:template>
<!-- Knoten mit Elternknoten -->
<xsl:template match="*/*" mode="dbg:expand-path">
<xsl:apply-templates select=".." mode="dbg:expand-path"/>/<xsl:value-ofselect="name( )"/>[<xsl:number/>]<xsl:text/>
</xsl:template>
<!-- Attributknoten -->
<xsl:template match="@*" mode="dbg:expand-path">
<xsl:apply-templates select=".." mode="dbg:expand-path"/>/@<xsl:value-of select="name( )"/>
</xsl:template>
<!-- Textknoten (der Übersicht wegen normalisiert) -->
<xsl:template match="text( )" mode="dbg:expand-path">normalized-text(<xsl:value-of select="normalize-space(.)"/>)</xsl:template>
</xsl:stylesheet>Calls to dbg:xtrace produce a message containing the current node’s path. This code is an identity stylesheet with a xtrace:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dbg="http://www.ora.com/XSLTCookbook/ns/debug">
<xsl:include href="xtrace.xslt"/>
<xsl:template match="/ | node( ) | @* | comment( ) | processing-instruction( )">
<xsl:call-template name="dbg:trace"/>
<xsl:copy>
<xsl:apply-templates select="@* | node( )"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>This test input:
<test foo="1">
<someElement n="1"/>
<someElement n="2">
<someChild>someValue</someChild>
</someElement>
<someElement n="3">
<someChild>someOtherValue</someChild>
</someElement>
<someElement n="4">
<someChild>someValue</someChild>
</someElement>
</test>produces this debugging output:
xtrace: /
xtrace: /test
xtrace: /test/@foo
xtrace: normalized-text( )
xtrace: /test/someElement[1]
xtrace: /test/someElement[1]/@n
xtrace: normalized-text( )
xtrace: /test/someElement[2]
xtrace: /test/someElement[2]/@n
xtrace: normalized-text( )
xtrace: /test/someElement[2]/someChild[1]
xtrace: normalized-text(someValue)
xtrace: normalized-text( )
xtrace: normalized-text( )
xtrace: /test/someElement[3]
xtrace: /test/someElement[3]/@n
xtrace: normalized-text( )
xtrace: /test/someElement[3]/someChild[1]
xtrace: normalized-text(someOtherValue)
xtrace: normalized-text( )
xtrace: normalized-text( )
xtrace: /test/someElement[4]
xtrace: /test/someElement[4]/@n
xtrace: normalized-text( )
xtrace: /test/someElement[4]/someChild[1]
xtrace: normalized-text(someValue)
xtrace: normalized-text( )
xtrace: normalized-text( )XSLT 2.0
Calls to dbg:trace can be enabled or disabled with attribute use-when, just as with xsl:message in recipe Using xsl:message effectively, provided your processor supports testing system properties.
Saxon 8.4 adds further tracing options:
-TJEnables tracing of external Java method binding. This helps explain why Saxon cannot find the Java method for a stylesheet extension function, or why it selects one method over another.
-TLclassnameRuns the stylesheet with the specified
TraceListener, whereclassnamenames a custom class implementingnet.sf.saxon.trace.TraceListener.-TPRuns the stylesheet with the
TraceListener TimedTraceListener, producing a file of timings for every executed instruction. Analyze this file to profile the stylesheet.
See Saxon documentation for details.
Discussion
Combine this technique with messages showing the current place in the stylesheet. Use a separate message, or parameter xtrace named tag, which, if set, is output instead of the default tag.
This example normalizes text nodes so trace output stays on one line. Remove the filter to see the full text.
Place trace calls carefully, ideally where the current node is actually processed. Consider this postorder stylesheet from recipe Performing a postorder traversal, instrumented here:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dbg="http://www.ora.com/XSLTCookbook/ns/debug">
<xsl:include href="xtrace.xslt"/>
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/employee" priority="10">
<xsl:apply-templates/>
<xsl:call-template name="dbg:trace"/>
<xsl:value-of select="@name"/>
<xsl:text> ist der Chef des Unternehmens. </xsl:text>
<xsl:call-template name="reportsTo"/>
<xsl:call-template name="HimHer"/>
<xsl:text>. </xsl:text>
<xsl:text>

</xsl:text>
</xsl:template>
<xsl:template match="employee[employee]">
<xsl:apply-templates/>
<xsl:call-template name="dbg:trace"/>
<xsl:value-of select="@name"/>
<xsl:text> ist ein Manager. </xsl:text>
<xsl:call-template name="reportsTo"/>
<xsl:call-template name="HimHer"/>
<xsl:text>. </xsl:text>
<xsl:text>

</xsl:text>
</xsl:template>
<xsl:template match="employee">
<xsl:call-template name="dbg:trace"/>
<xsl:text>Niemand berichtet </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>. 
</xsl:text>
</xsl:template>
<!-- Rest ausgelassen ... -->
</xsl:stylesheet>The trace runs when processing the current node, rather than as every template’s first instruction. This placement yields the correct postorder sequence:
xtrace: /employee/employee[1]/employee[1]
xtrace: /employee/employee[1]/employee[2]/employee[1]
xtrace: /employee/employee[1]/employee[2]
xtrace: /employee/employee[1]
xtrace: /employee/employee[2]/employee[1]
xtrace: /employee/employee[2]/employee[2]/employee[1]
xtrace: /employee/employee[2]/employee[2]/employee[2]
xtrace: /employee/employee[2]/employee[2]/employee[3]
xtrace: /employee/employee[2]/employee[2]
xtrace: /employee/employee[2]
xtrace: /employee/employee[3]/employee[1]/employee[1]
xtrace: /employee/employee[3]/employee[1]/employee[2]
xtrace: /employee/employee[3]/employee[1]/employee[3]
xtrace: /employee/employee[3]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[1]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[2]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[2]/employee[2]
xtrace: /employee/employee[3]/employee[2]/employee[2]
xtrace: /employee/employee[3]/employee[2]/employee[3]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[3]
xtrace: /employee/employee[3]/employee[2]
xtrace: /employee/employee[3]
xtrace: /employeePutting it on the first line would give this misleading preorder traversal instead:
xtrace: /employee
xtrace: /employee/employee[1]
xtrace: /employee/employee[1]/employee[1]
xtrace: /employee/employee[1]/employee[2]
xtrace: /employee/employee[1]/employee[2]/employee[1]
xtrace: /employee/employee[1]/employee[2]
xtrace: /employee/employee[1]
xtrace: /employee/employee[2]
xtrace: /employee/employee[2]/employee[1]
xtrace: /employee/employee[2]/employee[2]
xtrace: /employee/employee[2]/employee[2]/employee[1]
xtrace: /employee/employee[2]/employee[2]/employee[2]
xtrace: /employee/employee[2]/employee[2]/employee[3]
xtrace: /employee/employee[2]/employee[2]
xtrace: /employee/employee[2]
xtrace: /employee/employee[3]
xtrace: /employee/employee[3]/employee[1]
xtrace: /employee/employee[3]/employee[1]/employee[1]
xtrace: /employee/employee[3]/employee[1]/employee[2]
xtrace: /employee/employee[3]/employee[1]/employee[3]
xtrace: /employee/employee[3]/employee[1]
xtrace: /employee/employee[3]/employee[2]
xtrace: /employee/employee[3]/employee[2]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[1]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[2]
xtrace: /employee/employee[3]/employee[2]/employee[2]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[2]/employee[2]
xtrace: /employee/employee[3]/employee[2]/employee[2]
xtrace: /employee/employee[3]/employee[2]/employee[3]
xtrace: /employee/employee[3]/employee[2]/employee[3]/employee[1]
xtrace: /employee/employee[3]/employee[2]/employee[3]
xtrace: /employee/employee[3]/employee[2]
xtrace: /employee/employee[3]See also
CatchXSL is a free tool providing processor-dependent timing of XSL transformations. It records instructions during transformation as timestamped style events. The statistics describe transformation progress and suggest improvements. Detailed style-event listings show each step’s duration, while per-template listings reveal time-consuming hot spots.
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