Adding a Conditional Debug Mode

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

Many programming languages have a way to run a program in debug mode, which is typically used as a way to obtain additional information about run-time conditions (conditions at the time the transformation occurs). You can add this kind of debug code to your stylesheet, but before you can use it for production, you have to either remove the debug instructions or else comment them out, both of which are inefficient and may lead to the introduction of other, unexpected bugs .

However, you can add a debug mode to your stylesheets and have the debug code conditionally execute without modifying the stylesheet itself. An xsl:param element is the element of choice for this task, because XSLT allows you to specify its value outside the stylesheet at the time youre performing the transformation.

To do so, set up your stylesheet by defining a parameter named debug that is given the value of off :

 <xsl:param name="debug">off</xsl:param> 

xsl:choose is a good instruction to add to your stylesheet for testing the value of the debug parameter. If it has the value of 'on' , then perform the debug instructions in xsl:when , if not, then execute the regular xsl: otherwise instructions:

 <xsl:choose> <xsl:when test="$debug = 'on'"> <!-- If debug is on, do this --> </xsl:when> <xsl:otherwise> <!-- Otherwise, do this --> </xsl:otherwise> </xsl:choose> 

Consider the following use of a conditional debug mode. When debug mode is on, I output a lot of extra debugging information to the result document. Specifically, I add a debugdata element prior to the rest of the result. I stuff this element with debugging information. I also nest the whole result document in a debugmode element and nest the normal results inside a document element. Further, I add a conditional test to ensure that the processedby parameter is specified. The stylesheet is provided here:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="debug">off</xsl:param> <xsl:param name="processedby">none</xsl:param> <!-- Test for Debug mode --> <xsl:template match="/"> <!-- Force a processedby param to be specified --> <xsl:if test="$processedby = 'none'"> <xsl:message terminate="yes">No processedby parameter specified. Unable to continue.</xsl:message> </xsl:if> <xsl:choose> <!-- If debug is on, then add extra debug info --> <xsl:when test="$debug = 'on'"> <debugmode> <xsl:comment>*** Debug Mode - For Testing Purposes Only ***</xsl:comment> <debugdata> <processedby><xsl:value-of select="$processedby"/></processedby> <namespace-uri><xsl:value-of select="namespace- uri()"/></namespace-uri> <regioncount><xsl:value-of select="count(//region)"/></regioncount> <coffeecount><xsl:value-of select="count(//coffee)"/></coffeecount> </debugdata> <document> <xsl:apply-templates/> </document> </debugmode> </xsl:when> <!-- If debug is off, process as normal --> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- Copy everything else over --> <xsl:template match="@*node()"> <xsl:copy> <xsl:apply-templates select="@*node()"/> </xsl:copy> </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 specify the debug parameter to be on at process time (see Chapter 8 to find out about how to do this), I get the following result:

 <?xml version="1.0" encoding="utf-8"?> <debugmode> <!--*** Debug Mode - For Testing Purposes Only ***--> <debugdata> <processedby>$processedby</processedby> <namespace-uri/> <regioncount>2</regioncount> <coffeecount>4</coffeecount> </debugdata> <document> <coffees> <region name="Latin America"> <coffee> <taste>Mild and Bland</taste> <price>11.99</price> <availability>Year-round</availability> <bestwith>Breakfast</bestwith> <discountprice>9.59</discountprice></coffee> <coffee> <taste>Exotic and Untamed</taste> <price>12.99</price> <availability>Year-round</availability> <bestwith>Dessert</bestwith> <discountprice>10.39</discountprice></coffee> </region> <region name="Africa"> <coffee> <taste>Exotic and Untamed</taste> <price>14.99</price> <availability>Limited</availability> <bestwith>Chocolate</bestwith> <discountprice>11.99</discountprice></coffee> <coffee> <taste>Solid yet Understated</taste> <price>3.99</price> <availability>Year-round</availability> <bestwith>Elephant Ears</bestwith> <discountprice>3.19</discountprice></coffee> </region> </coffees> </document> </debugmode> 
  
 
 
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