Conditionally Halting Execution

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

A debugging technique that can be worthwhile is to halt execution of your transformation when certain conditions occur at the time of processing. If those conditions occur, the processor can flag you with an xsl:message instruction and you can optionally decide whether or not to terminate processing.

Consider the coffee.xml source code in Listing 17-1.

Listing 17-1: coffee.xml
start example
 <?xml version="1.0"?> <coffees> <region name="Latin America"> <coffee name="Guatemalan Express" origin="Guatemala"> <taste>Mild and Bland</taste> <price>11.99</price> <availability>Year-round</availability> <bestwith>Breakfast</bestwith> </coffee> <coffee name="Costa Rican Deacon" origin="Costa Rica"> <taste>Exotic and Untamed</taste> <price>12.99</price> <availability>Year-round</availability> <bestwith>Dessert</bestwith> </coffee> </region> <region name="Africa"> <coffee name="Ethiopian Sunset Supremo" origin="Ethiopia"> <taste>Exotic and Untamed</taste> <price>14.99</price> <availability>Limited</availability> <bestwith>Chocolate</bestwith> </coffee> <coffee name="Kenyan Elephantismo" origin="Kenya"> <taste>Solid yet Understated</taste> <price>3.99</price> <availability>Year-round</availability> <bestwith>Elephant Ears</bestwith> </coffee> </region> </coffees> 
end example
 

When I transform this source document, I want to check the price of each coffee element to be sure that the price is valid. If the price is below $6, then I want to stop the transformation, because a data error must have occurred. To set this up, I create a template rule for the coffee element:

 <!-- Check price of coffee before continuing --> <xsl:template match="coffee"> <xsl:if test="price &lt; 6"> <xsl:message terminate="yes"> ERROR: Hold on there Tex. Price should never be under .00. Price for <xsl:value-of select="@name"/> is <xsl:value-of select="price"/>.  </xsl:message> </xsl:if> <xsl:apply-templates select="price"/> </xsl:template> 

Inside this template rule, I use an xsl:if instruction to test the price of coffee. If it is less than $6, then xsl:message gives the error text and stops the execution of the process.

The entire XSLT stylesheet is shown here:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Check price of coffee before continuing --> <xsl:template match="coffee"> <xsl:if test="price &lt; 6"> <xsl:message terminate="yes"> ERROR: Hold on there Tex. Price should never be under .00. Price for <xsl:value-of select="@name"/> is <xsl:value-of select="price"/>. </xsl:message> </xsl:if> <xsl:apply-templates select="price"/> </xsl:template> <!-- Add new element --> <xsl:template match="price"> Today's price for <xsl:value-of select="../@name"/> is <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> 

When this stylesheet is applied to the coffee.xml file (refer to Listing 17-1), the Kenyan Elephantismo coffee causes the xsl:if instruction to trigger, so the XSLT processor halts and reports the following error:

 ERROR: Hold on there Tex. Price should never be under .00. Price for Kenyan Elephantismo is 3.99. 
  
 
 
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