Tracing through Your Code

 
xslt for dummies
Chapter 17 - Debugging XSLT Transformations
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

Outside a full-scale debugger, the best way to obtain detailed information about how the XSLT processor is transforming your XML document is to trace through each of the transformation steps. Tracing is a debugging technique that gives you a step-by-step account of the transformation taking place. XSLT itself has no support for stepping through your stylesheet like this, but many XSLT processors add tracing as a feature. Take, for example, the SAXON processor. It has a specific option that you can turn on to output tracing information. So, suppose I apply the following stylesheet to the coffee.xml document shown in Listing 17-1:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="coffees"> <coffees> <xsl:apply-templates/> </coffees> </xsl:template> <!-- Add new element --> <xsl:template match="coffee"> <coffee> <xsl:apply-templates/> <discountprice><xsl:value-of select="format-number(price*.8, '##.##')"/></discountprice> </coffee> </xsl:template> </xsl:stylesheet> 

When I run the SAXON processor from a command line, I can turn trace support on by specifying the -T option:

 saxon -T -o c:\output.txt coffee.xml coffee_tracer.xsl 

In addition to generating the normal result document, the processor also spits out the following tracing information to the command line. Notice that the information is captured in an XML structure, so you can analyze it by using your XML and XSLT tools if you want:

 <trace> <Top-level element="xsl:template" line="3" file="file:/C:/Xslt/coffee_tracer.xsl" precedence="0"/> <Top-level element="xsl:template" line="10" file="file:/C:/Xslt/coffee_tracer.xsl" precedence="0"/> <Source node="/coffees[1]" line="2" mode="*default*"> <Instruction element="xsl:template" line="3"> <Instruction element="coffees" line="4"> <Instruction element="xsl:apply-templates" line="5"> <Source node="/coffees[1]/region[1]/coffee[1]" line="4" mode="*default*"> <Instruction element="xsl:template" line="10"> <Instruction element="coffee" line="11"> <Instruction element="xsl:apply-templates" line="12"> </Instruction> <!-- xsl:apply-templates --> <Instruction element="discountprice" line="13"> <Instruction element="xsl:value-of" line="13"> </Instruction> <!-- xsl:value-of --> </Instruction> <!-- discountprice --> </Instruction> <!-- coffee --> </Instruction> <!-- xsl:template --> </Source><!-- /coffees[1]/region[1]/coffee[1] --> <Source node="/coffees[1]/region[1]/coffee[2]" line="10" mode="*default*"> <Instruction element="xsl:template" line="10"> <Instruction element="coffee" line="11"> <Instruction element="xsl:apply-templates" line="12"> </Instruction> <!-- xsl:apply-templates --> <Instruction element="discountprice" line="13"> <Instruction element="xsl:value-of" line="13"> </Instruction> <!-- xsl:value-of --> </Instruction> <!-- discountprice --> </Instruction> <!-- coffee --> </Instruction> <!-- xsl:template --> </Source><!-- /coffees[1]/region[1]/coffee[2] --> <Source node="/coffees[1]/region[2]/coffee[1]" line="18" mode="*default*"> <Instruction element="xsl:template" line="10"> <Instruction element="coffee" line="11"> <Instruction element="xsl:apply-templates" line="12"> </Instruction> <!-- xsl:apply-templates --> <Instruction element="discountprice" line="13"> <Instruction element="xsl:value-of" line="13"> </Instruction> <!-- xsl:value-of --> </Instruction> <!-- discountprice --> </Instruction> <!-- coffee --> </Instruction> <!-- xsl:template --> </Source><!-- /coffees[1]/region[2]/coffee[1] --> <Source node="/coffees[1]/region[2]/coffee[2]" line="24" mode="*default*"> <Instruction element="xsl:template" line="10"> <Instruction element="coffee" line="11"> <Instruction element="xsl:apply-templates" line="12"> </Instruction> <!-- xsl:apply-templates --> <Instruction element="discountprice" line="13"> <Instruction element="xsl:value-of" line="13"> </Instruction> <!-- xsl:value-of --> </Instruction> <!-- discountprice --> </Instruction> <!-- coffee --> </Instruction> <!-- xsl:template --> </Source><!-- /coffees[1]/region[2]/coffee[2] --> </Instruction> <!-- xsl:apply-templates --> </Instruction> <!-- coffees --> </Instruction> <!-- xsl:template --> </Source><!-- /coffees[1] --> </trace> 
  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net